Indexing multidimensional array

550 views Asked by At

Is it possible in Java to index an Array somehow like arr1[arr2] if arr1 is an int[][] and arr2 an int[2].

I would like a better way to achieve the same as arr1[arr2[0]][arr2[1]] which looks really awfull. And by better I mean easier to read and understand.

3

There are 3 answers

3
Jeff Scott Brown On BEST ANSWER

Is it possible in Java to index an Array somehow like arr1[arr2] if arr1 is an int[][] and arr2 an int[2]

arr1[arr2] is not valid if arr2 is an int[]. The only thing that can go in between the square brackets is a number (or a variable that references the number). arr1[arr2[0]] is valid (assuming both arrays have at least 1 element in them) because arr2[0] would be an int.

EDIT

After I posted this answer the question was changed to include "Edit: I want to achieve the same as arr1[arr2[0]][arr2[1]]".

The way to achieve what you have shown, is exactly what you have shown, assuming arr1 is a int[][], arr2 is a int[], arr1 has at least 1 element in the first dimension and its value is an array that has at least 2 elements.

2nd EDIT

You said "I would like a better way for achieving arr1[arr2[0]][arr2[1]]". "better" is subjective. if you want better in terms of fewer lines of code, it won't get better because you have the minimum lines of code (1). If you want it to be easier to read, you could do something like this:

// of course, knowing the nature of the data
// would allow better variable names...
int firstElementInArr2 = arr2[0];
int secondElementInArr2 = arr2[1];
int[] outerArray = arr1[firstElementInArr2];
int result = outerArray[secondElementInArr2];
0
AudioBubble On

If you have a rectangular 2d array of known dimensions m×n, then you can supplement it with the getValue and setValue methods and access its elements using one variable position in the range from 1 to m×n.

Assignment, Arithmetic, and Unary Operators

  • / - Division operator
  • % - Remainder operator
public class Test {
    static int m = 3, n = 4;
    static int[][] array = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

    public static void main(String[] args) {
        System.out.println(getValue(5));     // 5
        System.out.println(setValue(5, 90)); // true
        System.out.println(getValue(5));     // 90
        System.out.println(Arrays.deepToString(array));
        // [[1, 2, 3, 4], [90, 6, 7, 8], [9, 10, 11, 12]]
    }

    public static int getValue(int pos) {
        if (pos >= 1 && pos <= m * n) {
            int i = (pos - 1) / m; // row
            int j = (pos - 1) % n; // column
            return array[i][j];
        } else {
            return 0;
        }
    }

    public static boolean setValue(int pos, int value) {
        if (pos >= 1 && pos <= m * n) {
            int i = (pos - 1) / m; // row
            int j = (pos - 1) % n; // column
            array[i][j] = value;
            return true;
        } else {
            return false;
        }
    }
}

See also: Easier way to represent indicies in a 2D array

2
WJS On

Another thing you can do which is to remember that multi-dimensional arrays are arrays of arrays...

So you can do the following:

int[][] arr = {{1,2,3},{4,5,6}};

int[] a = arr[1];
System.out.println(Arrays.toString(a));

Prints

[4, 5, 6]


a[1] = 99;
System.out.println(Arrays.deeptoString(arr);

Prints

[[1, 2, 3], [4, 99, 6]]