划分阵列成较小的部分 [英] divide array into smaller parts
本文介绍了划分阵列成较小的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个大的字节数组转换为更小的块(例如64字节)。请帮我这个。
I would like to divide a large byte array into smaller chunks (say 64 bytes). Please help me with this.
推荐答案
您可以使用方法Arrays.copyOfRange(原件,从,到)
You can use the method 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;
}
或者您可以使用作为最大建议System.arraycopy
Or You can use as Max suggested the 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;
}
这篇关于划分阵列成较小的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文