我想将一个大的字节数组分成更小的数据块(比如64个字节)。请帮我解决一下这个。将数组分成较小的部分
回答
您可以使用该方法Arrays.copyOfRange(原从,到)
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
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;
}
它对我真的很有帮助。谢谢 – 2012-07-21 07:32:58
请注意,如果source.length不是偶数的偶数倍,那么其中的第二个似乎会分配最后一个块的“太大”... – rogerdpack 2015-05-06 23:45:58
见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);
你有两个选择:
System.arraycopy(...)
Array.copyOfRange(...)
两者的工作方式相同,但是当第一个只可管理拷贝,第二个,就是要用于同时分配新块。
我一个结果System.arraycopy
为基准,它们更快,如果你管理你的拆分数组,但如果你将它们分配whle复制稍慢之前分配块一起:在这种情况下,你应该使用Array.copyOfRange
。
Array.copyOfRange()调用System.arraycopy时非常有趣的基准测试:http://pastebin.com/SpSyx8Cd – bezmax 2010-08-04 12:56:10
这样做......
byte[] source = new byte[2048];
byte[] target = new byte[1024];
// fill source with some data...
Array.Copy(source, buffer, 1024);
达米安VASH的第一个方法添加零到最后块结束时,如果输入不是CHUNKSIZE的准确的倍数(使用Arrays.copyOfRange()一) 。
您可能要改用这一点:
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;
}
如果你正在寻找节省一些内存,略有修改达米安瓦什的答案将有所帮助(在这种情况下,任何剩余的块没有分配完整的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;
}
- 1. 将数组拆分成定义值的较小部分
- 2. 将StringBuffer分成较小的部分
- 3. 将逗号分隔的字符串分成较小的部分
- 4. Python - 将numpy数组分割成不同大小的部分
- 5. Coldfusion将数组分成两部分
- 6. 将数组分解成小数组
- 7. 将一组数字分成两部分,其中差异最小
- 8. 将QImage划分成更小的部分
- 9. 将较大的malloc块划分成较小的“分区”
- 10. 将int数组拆分为3部分,最大化2个较小部分的大小
- 11. 将大页面分解成小部分
- 12. 将较大的views.py分成小块
- 13. 将mol2分子的数据库拆分为N个较小组
- 14. 分阵列分成较小的阵列
- 15. 如何将数组分成小阵列?
- 16. 组成部分
- 17. memcpy将较小的数组连接成较大的数组
- 18. 将数组分成3组
- 19. 将大型JSON拆分为Javascript/jQuery中的较小部分
- 20. 小数部分的小数群组分隔符
- 21. 根据密钥的值将散列数组分成两部分
- 22. 如何将Perl数组分成更小的数组?
- 23. 用相同的元素将数组分成小数组
- 24. php将数组拆分成更小的偶数组
- 25. numpy数组的小数部分
- 26. 将剩余分割字节数组(未知大小)分块成
- 27. 将6位整数分成3部分?
- 28. 如何将点分成两组 - 分布的上下部分
- 29. 将wordpress分成两部分
- 30. 将ListView分割成部分
听起来类似于http://stackoverflow.com/questions/3395547/how-to-get-a-sub-array-of-array-in-java-without-copying-data/ – 2010-08-04 12:44:19