Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在字节[]和整数[]上运行,内存相同_Java_Android_Performance - Fatal编程技术网

Java 在字节[]和整数[]上运行,内存相同

Java 在字节[]和整数[]上运行,内存相同,java,android,performance,Java,Android,Performance,我希望加快我的程序。目前,我有一个函数可以执行以下操作: public void updateBitmap(byte[] buf, int thisPacketLength, int standardOffset, int thisPacketOffset) { int pixelCoord = thisPacketOffset / 3 - 1; for (int bufCoord = standardOffset; bufCoord < thisPacketL

我希望加快我的程序。目前,我有一个函数可以执行以下操作:

public void updateBitmap(byte[] buf, int thisPacketLength, int standardOffset, int thisPacketOffset) {      
    int pixelCoord = thisPacketOffset / 3 - 1;
    for (int bufCoord = standardOffset; bufCoord < thisPacketLength; bufCoord += 3) {
        pixelCoord++;
        pixelData[pixelCoord] = 0xFF << 24 | (buf[bufCoord + 2] << 16) & 0xFFFFFF | (buf[bufCoord + 1] << 8) & 0xFFFF | buf[bufCoord] & 0xFF;           
    }
}   
public void updateBitmap(字节[]buf,int thisPacketLength,int standardcoffset,int thisPacketOffset){
int-pixelCoord=thisPacketOffset/3-1;
对于(int-bufCoord=standardOffset;bufCoordpixelData[pixelCoord]=0xFF您应该改用字节缓冲区。然后您可以将其作为int缓冲区访问,并逐字节或逐int读取/写入字节

使用以下内容创建它:

ByteBuffer bb = ByteBuffer.wrap(buf);
并获取IntBuffer:

IntBuffer ib = bb.asIntBuffer();
您可以通过写入索引来设置此缓冲区中的值:

ib.put(2, 400);

WARE 2是索引,400是值。对
ib
的任何更改都将得到
bb
buf
的支持。请不要忘记,这样做可能会导致端性问题。幸运的是,
ByteBuffer
允许选择
ByteOrder
@maaartinus。我从来不知道这一点。谢谢通知我们!不幸的是在我的android设备上,我在int[]test=ib.array()上得到java.lang.UnsupportedOperationException。我需要有一个整数数组本身的引用,而不仅仅是能够使用IntBuffer修改它。有什么建议吗?@Trevor你可以将它逐int读取到一个相同大小的数组中。是的,我可以这样做,但这会破坏我所做的目的。我想让int数组和字节数组指向相同的内存。那么做byte[0]=…将对int[0]产生影响,反之亦然