0

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 Answers3

3

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];
Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • I am sorry, I didn't exactly express what I wanted to do. I want to achieve the same as `arr1[arr2[0]][arr2[1]]` which looks really awful. – Michael Hofmann Apr 12 '21 at 19:54
  • "I want to achieve the same as arr1[arr2[0]][arr2[1]] which looks really awful" - There may be other languages that have a different syntax. That is the syntax in Java though. You could break it into multiple expressions if you wanted, but that code represents how it would be done. – Jeff Scott Brown Apr 12 '21 at 19:55
  • I thought chances are low that there is an other way to do this. But because you should never stop learning and because there could be a method, I posted this question. Thanks for your answer though! – Michael Hofmann Apr 12 '21 at 20:02
0

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]]
WJS
  • 36,363
  • 4
  • 24
  • 39
  • I know that multi-dimensional arrays are arrays of arrays of arrays ... I also fully understanding one-dimensional arrays and have no problem with arrays of higher dimensions. But how is this going to help me in this case? – Michael Hofmann Apr 12 '21 at 20:15
  • More information and you were also asking about a different way of indexing a `"2D"` array. If you didn't know this, then it may be beneficial, now or in the future. – WJS Apr 12 '21 at 21:15
0

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