复制java数组“over”另一个

时间:2015-09-08 12:31:23

标签: java arrays

我想知道是否真的 - 正如我的搜索所示 - 无法将一个数组的字节副本执行到另一个不同基本类型的数组中。 我有一个来自文件的byte [],表示内存中的int []。我可以做像

这样的转变
myints[i] = (mybytes[0] << 24) || (mybytes[1] << 16) || (mybytes[2] << 8) || mybytes[0];

但这种性能杀手不是首选方式?没有这样的东西吗?

byte[] mybytes = ... coming from file  
int[] myints = new int[mybytes.length / 4];  
MagicCopyFunction: copy mybytes.length bytes from 'mybytes' into 'myints'

1 个答案:

答案 0 :(得分:4)

最简单的方法是使用Buffer s。

ByteBuffer b = ByteBuffer.allocate(1024);
// Fill the bytebuffer and flip() it
IntBuffer i = b.asIntBuffer();