将数组分成更小的部分

时间:2010-08-04 11:51:28

标签: java arrays

我想将一个大字节数组分成更小的块(比如64字节)。请帮我解决一下这个。

8 个答案:

答案 0 :(得分:14)

您可以使用方法Arrays.copyOfRange(original,from,to)

 public static byte[][] divideArray(byte[] source, int chunksize) {


        byte[][] ret = new byte[(int)Math.ceil(source.length / (double)chunksize)][chunksize];

        int start = 0;

        for(int i = 0; i < ret.length; i++) {
            ret[i] = Arrays.copyOfRange(source,start, start + chunksize);
            start += chunksize ;
        }

        return ret;
    }

或者您可以使用Max建议的System.arraycopy

public static byte[][] divideArray(byte[] source, int chunksize) {


        byte[][] ret = new byte[(int)Math.ceil(source.length / (double)chunksize)][chunksize];

        int start = 0;

        for(int i = 0; i < ret.length; i++) {
            if(start + chunksize > source.length) {
                System.arraycopy(source, start, ret[i], 0, source.length - start);
            } else {
                System.arraycopy(source, start, ret[i], 0, chunksize);
            }
            start += chunksize ;
        }


        return ret;
    }

答案 1 :(得分:11)

Damian Vash的第一个方法(使用Arrays.copyOfRange()的方法)如果输入不是chunksize的倍数,则将零添加到最后一个块的末尾。

您可能想要使用此代码:

public static List<byte[]> divideArray(byte[] source, int chunksize) {

    List<byte[]> result = new ArrayList<byte[]>();
    int start = 0;
    while (start < source.length) {
        int end = Math.min(source.length, start + chunksize);
        result.add(Arrays.copyOfRange(source, start, end));
        start += chunksize;
    }

    return result;
}

如果它有用,使用ArrayList的相同的东西:

  public static List<List<String>> divideList(List<String> source, int chunksize) {
    List<List<String>> result = new ArrayList<List<String>>();
    int start = 0;
    while (start < source.size()) {
      int end = Math.min(source.size(), start + chunksize);
      result.add(source.subList(start, end));
      start += chunksize;
    }
    return result;
  }

答案 2 :(得分:3)

如果您正在寻找保存一些内存,稍微修改Damian Vash的答案会有所帮助(在这种情况下,任何剩余的块都没有分配完整的64字节块大小......)

private byte[][] splitChunks(byte[] source)
{
    byte[][] ret = new byte[(int)Math.ceil(source.length / (double)CHUNK_SIZE)][];
    int start = 0;
    for(int i = 0; i < ret.length; i++) {
        if(start + CHUNK_SIZE > source.length) {
            ret[i] = new byte[source.length-start];
            System.arraycopy(source, start, ret[i], 0, source.length - start);
        } 
        else {
            ret[i] = new byte[CHUNK_SIZE];
            System.arraycopy(source, start, ret[i], 0, CHUNK_SIZE);
        }
        start += CHUNK_SIZE ;
    }
    return ret;
}

答案 3 :(得分:2)

嗯,System.arraycopy(src,fromPos,dest,toPos,length)通常被认为比Arrays.copyOfRange更快。

byte[] source = ...read it from somewhere...;
byte[] newArray = new byte[64];
System.arraycopy(source, 0, newArray, 0, 64);

答案 4 :(得分:1)

您有两种选择:

  • System.arraycopy(...)
  • Array.copyOfRange(...)

它们都以相同的方式工作,但是第一个只管理复制,第二个用于同时分配新块。

我对它们进行了基准测试,结果是System.arraycopy如果你在分割数组之前管理所有块一起分配更快,但是如果你在复制时分配它们会稍慢:在这种情况下你应该使用{{1} }。

答案 5 :(得分:0)

请参阅Arrays.copyOfRange寻求帮助。您可以在循环中使用它将数组拆分为几个较小的块。

答案 6 :(得分:0)

这样做......

    byte[] source = new byte[2048];
    byte[] target = new byte[1024];  

// fill source with some data...

    Array.Copy(source, buffer, 1024);

答案 7 :(得分:0)

这是使用 Arrays.copyOfRange 将字节数组分割成块的另一种可能方式(将“数据”分割成 blockSize 块):

byte[] data = { 2, 3, 5, 7, 8, 9, 11, 12, 13 };
// Block size in bytes (default: 64k)
int blockSize = 64 * 1024;
int blockCount = (data.length + blockSize - 1) / blockSize;

byte[] range;

try {
  
    for (int i = 1; i < blockCount; i++) {
        int idx = (i - 1) * blockSize;
        range = Arrays.copyOfRange(data, idx, idx + blockSize);
        System.out.println("Chunk " + i + ": " Arrays.toString(range));
    }

} finally {
    
    // Last chunk
    int end = -1;
    if (data.length % blockSize == 0) {
            end = data.length;
    } else {
            end = data.length % blockSize + blockSize * (blockCount - 1);
    }
        
    range = Arrays.copyOfRange(data, (blockCount - 1) * blockSize, end);

    System.out.println("Chunk " + blockCount + ": " Arrays.toString(range));
}
相关问题