Java 将数组分成更小的部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3405195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
divide array into smaller parts
提问by malak
I would like to divide a large byte array into smaller chunks (say 64 bytes). Please help me with this.
我想将一个大字节数组分成更小的块(比如 64 个字节)。请帮我解决一下这个。
回答by Damian Leszczyński - Vash
You can use the method Arrays.copyOfRange(original, from, to)
您可以使用方法 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;
}
Or You can use as Max suggested the System.arraycopy
或者你可以像 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;
}
回答by moxn
See Arrays.copyOfRangefor help. You could use this in a loop to split your array into several smaller chunks.
请参阅Arrays.copyOfRange以获取帮助。您可以在循环中使用它来将数组拆分为几个较小的块。
回答by bezmax
Well, System.arraycopy(src, fromPos, dest, toPos, length) is generally considered faster than Arrays.copyOfRange.
好吧,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);
回答by Hyman
You have two choices:
你有两个选择:
System.arraycopy(...)
Array.copyOfRange(...)
System.arraycopy(...)
Array.copyOfRange(...)
both of them work the same way but while first one only manages copy, second one is meant to be used to allocate the new chunk at the same time.
它们的工作方式相同,但第一个只管理副本,第二个用于同时分配新块。
I benchmarked them with a result that System.arraycopy
is faster if you manage to allocate chunks all together before splitting your array but slightly slower if you allocate them whle you copy: in this case you should use Array.copyOfRange
.
System.arraycopy
如果您在拆分数组之前设法将块分配在一起,我对它们进行了基准测试,结果会更快,但如果您在复制时分配它们,则速度会稍慢一些:在这种情况下,您应该使用Array.copyOfRange
.
回答by Nooneelse
This will do...
这会做...
byte[] source = new byte[2048];
byte[] target = new byte[1024];
// fill source with some data...
Array.Copy(source, buffer, 1024);
回答by Mirko Seifert
Damian Vash's first method (the one using Arrays.copyOfRange()) adds zeros to the end of the last chunk if the input is not exactly a multiple of chunksize.
Damian Vash 的第一个方法(使用 Arrays.copyOfRange() 的方法)如果输入不是块大小的整数倍,则会在最后一个块的末尾添加零。
You might want to use this instead:
您可能想改用它:
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;
}
and in case it's useful, the same thing using ArrayList's:
如果它有用,使用 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;
}
回答by uLYsseus
If you are looking save some memory, a slight modification to Damian Vash's answer would help (in this case any remaining chunk is not allocated a complete 64 byte block size, as well...)
如果您正在寻找节省一些内存,对 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;
}