Accessing Elements in 2D Arrays in Java

realedwintorres

Edwin Torres

Posted on November 28, 2021

Accessing Elements in 2D Arrays in Java

The two-dimensional (2D) array is a useful data structure. Like one-dimensional arrays, 2D arrays work well with for loops.

Here is a 2D array in Java:

int[][] arr = {
  {0, 1, 2, 3, 4},
  {5, 6, 7, 8, 9},
  {10, 11, 12, 13, 14}
};
Enter fullscreen mode Exit fullscreen mode

The int[][] declaration means that arr is a 2D array of int values. Since there are two pairs of [], the array has two dimensions.

Look at this 2D array. It is actually an array of arrays. The array arr has a length of 3. It has three elements, or rows. Each row is an array of length 5:

{0, 1, 2, 3, 4}

{5, 6, 7, 8, 9}

{10, 11, 12, 13, 14}
Enter fullscreen mode Exit fullscreen mode

The first array is arr[0]. The second and third arrays are arr[1] and arr[2] respectively. A for loop can access each of these arrays using an index:

for (int i=0; i < arr.length; i++) {
  int[] row = arr[i];
  System.out.println( Arrays.toString(row) );
}
Enter fullscreen mode Exit fullscreen mode

Here is the output:

[0, 1, 2, 3, 4]
[5, 6, 7, 8, 9]
[10, 11, 12, 13, 14]
Enter fullscreen mode Exit fullscreen mode

The for loop accesses each row. Since the row is also an array, an inner for loop can access each of its elements:

for (int i=0; i < arr.length; i++) {
  int[] row = arr[i];
  for (int j=0; j < row.length; j++) {
    System.out.println(i + "," + j + ": " + arr[i][j]);
  }
}
Enter fullscreen mode Exit fullscreen mode

Here is the output:

0,0: 0
0,1: 1
0,2: 2
0,3: 3
0,4: 4
1,0: 5
1,1: 6
1,2: 7
1,3: 8
1,4: 9
2,0: 10
2,1: 11
2,2: 12
2,3: 13
2,4: 14
Enter fullscreen mode Exit fullscreen mode

In the output, the first number is the row index. The second number is the column index. The third number is the element at that position of the 2D array.

Note how two indexes are required when accessing a single element of the 2D array. For example, arr[1][3] contains the int value 8. The for loop accesses each row in order. Once it has a row, then it uses an inner for loop to access its int values in order.

Here is a complete example:

public class Example {
  public static void main(String[] args) {

    int[][] arr = {
      {0, 1, 2, 3, 4},
      {5, 6, 7, 8, 9},
      {10, 11, 12, 13, 14}
    };

    for (int i=0; i < arr.length; i++) {
      int[] row = arr[i];
      for (int j=0; j < row.length; j++) {
        System.out.println(i + "," + j + ": " + arr[i][j]);
      }
    }

  }
}
Enter fullscreen mode Exit fullscreen mode

When using 2D arrays, use two indexes. The first index is for the row. The second index is for the column. In other words, each element of a 2D array is an array. The first index accesses an array element. Then use a second index to access the elements in that array. To facilitate this, use a for loop with an inner for loop to provide the row and column indexes.

Thanks for reading.

Follow me on Twitter @realEdwinTorres for more programming tips. 😀

💖 💪 🙅 🚩
realedwintorres
Edwin Torres

Posted on November 28, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Using Binary Search in Java
java Using Binary Search in Java

November 27, 2021

Basic Arrays in Java
java Basic Arrays in Java

November 10, 2021

Creating a Squid Game Card in HTML
javascript Creating a Squid Game Card in HTML

November 9, 2021