To change a value at particular index in an array


#1

For example 'm having an array of size n. Now 'm creating a pointer to the array. I need to change the value at a particular index using the pointer. Is this possible r not??? thanks in advance


#2

Hello

It’s not possible to modify an entry of an array through a Pointer on Java side. The Pointer class is solely intended for the communication with the native side and the representation of device pointers.

It might be possible to have a Pointer class that is more powerful and also allows modifications of the pointed-to data on Java side, but this might bear some difficulties (the most obvious one: What if the pointer points to device memory?).

Depending on what you are going to do, you might consider changing the array entry directly, or possibly use an IntBuffer. It can, in many ways, be used like a Pointer:

int array[] = { 0,1,2,3 };
IntBuffer buffer = IntBuffer.wrap(array);

buffer.position(2);
buffer.put(-1); // Now the array is { 0,1,-2,3 }

and of course, can be used in the Pointer#toBuffer method.

bye
Marco