Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Direct access to part of an array

Tags:

java

arrays

To speed up my image processing application, I'm trying to address values in an array starting in the middle, and using indices starting at 0.

For example, when I work with two arrays, I often have a situation like this:

public void work(int[] array1, int[] array2, int offset)
{
    int len = array1.length;
    for (int i = 0; i < len; i++)
        array1[i] += array2[i + offset];
}

so I would like to create a new variable, array3 that directly maps into the middle of array2 (not a copy), so I can do this:

public void work(int[] array1, int[] array2, int offset)
{
    int[] array3 = array2[offset]...;
    int len = array1.length;
    for (int i = 0; i < len; i++)
        array1[i] += array3[i];
}

In effect, I want a Java equivalent to this c statement:

int *array3ptr = array2ptr + offset;

Note: I don't have any experience with JNI or anything, so if it boils down to using this, please provide working examples.

like image 515
Mark Jeronimus Avatar asked Dec 06 '25 05:12

Mark Jeronimus


1 Answers

This is not possible to do by using pure Java arrays and the array[index] notation. In Java arrays can never overlap.

You could however wrap your arrays in IntBuffer. If you use a direct buffer (see allocateDirect) you should get real good performance.

A statement such as

int *array3ptr = array2ptr + offset;

would be written as

IntBuffer array3 = array2.slice().position(offset);

These

like image 179
aioobe Avatar answered Dec 07 '25 19:12

aioobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!