    # How do you traverse a 2D array?

In this post, we will look at how to use for loop with two dimensional array in Java. When we embed one for loop in another then it is known as nested for loop.

To loop over two dimensional array in Java you can use two for loops. Each loop uses an index. Index of outer for loop refers to the rows, and inner loop refers to the columns. You can then get each element from the array using the combination of row and column indexes.

Generally speaking, I have rarely seen more than 4-dimensional array, in most cases, two dimensional array solves the problem. Most of the problems that include board, matrix or grid can be solved using two dimensional array.  So it becomes very important to understand how to assign values to two dimensional array and loop over it.

Java doesn’t support multidimensional array by default. When we implement a 2d array, it is actually the arrays of an array.

## Program to demonstrate for loop with two dimensional array

```public class ForLoopExample {
public static void main(String[] args) {
int[][] values = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

System.out.println("Elements are :");
for(int i=0; i< values.length; i++) {
for(int j=0; j< values[i].length; j++) {
System.out.print(values[i][j] + "\t");
}
System.out.println("");
}
}
}
```

Output:

```Elements are :
1	2	3
4	5	6
7	8	9
```

Explanation:
The above program iterated each row and then for each row, we looped through each column.

Let’s try to visualize the iteration. Variable ‘i’ specifies rows and ‘j’ specifies columns, both of them together identifies an element. As ‘i’ progress we get hold of the next row. We loop through till the length of two dimensional array.

In case if you want to come out of a nested loop, you can use the break statement. The break statement terminates the loop and starts executing the next statement.

## Loop two dimensional array using enhanced for loop

```/**
* Program to demonstrate iterating two dimensional array using enhanced for
* loop
*/
public class TwoDimensionalArrayEnhancedForLoop {
public static void main(String[] args) {
int[][] contents = { { 88, 66, 79 }, { 56, 25, 39 }, { 58, 47, 69 } };

System.out.println("Loop Using Enhanced for loop:");
for (int[] eachRow : contents) {
for (int j : eachRow) {
System.out.print(j + "\t");
}
System.out.println("");
}
}
}
```

Output:

```Loop Using Enhanced for loop:
88	66	79
56	25	39
58	47	69
```

Explanation:
In the traditional method, we dealt with the index and fetched elements. But in enhanced for loop we get hold of the row, and for each row we get the values of elements.
When we say row, we get hold of int[] and then we can iterate this int[] and print or process each element.
Use enhanced for loop when you don’t want to know the index of the element you are currently processing. As we saw code becomes clear and concise when we use enhanced for loop.

Most of the time the multidimensional arrays are restricted to 2d and 3d. If you want to loop over ‘n’ dimensional array then you can have that many nested loop and process the elements.

You have learned a very useful concept of iterating a two dimensional array in Java. If you liked this article please share it.

We explored using `for` loops with one-dimensional arrays. Now let's jump into nested `for` loops as a method for iterating through 2D arrays. A nested `for` loop is one `for` loop inside another. Take a look below to see what this means.

Run & Edit in Smart IDE (Beta)

Iteration2DExample.java

``````package exlcode;

public class Iteration2DExample {

public static int[][] exampleVariableOne = {{0, 1, 2, 3, 4}, {4, 5, 6, 7, 8}};

public static void main(String[] args) {
// nested for loops are necessary for
// iterating through a 2D array
for (int countOne = 0; countOne < exampleVariableOne.length; countOne++) {
for (int countTwo = 0; countTwo < exampleVariableOne[countOne].length; countTwo++) {
System.out.print("Index [" + countOne + "][" + countTwo + "]: ");
System.out.println(exampleVariableOne[countOne][countTwo]);
}
}
}
}``````

The first `for` loop loops through each row of the 2D array one by one. As the first loop runs through each row, the second (nested) `for` loop inside the first loop loops through the columns one by one. The nested for loops runs row by row, checking each column within the row before moving on to the next row.

Because each row could have different numbers of columns, we need to access the specific column length of the specified row. That is why you see `exampleVariableOne[countOne].length` used within the second nested `for` loop.

The concept of using loops when working with 2D arrays is an essential tool in every programmer's toolkit. Look meticulously through the code above and become comfortable with how each loop fits into the big picture. When you become comfortable with the `for` loop, try using “for-each” loops with 2D arrays.

### How to traverse 2D array in C?

Two-dimensional array example in C.
#include<stdio.h>.
int main(){.
int i=0,j=0;.
int arr={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};.
//traversing 2D array..
for(i=0;i<4;i++){.
for(j=0;j<3;j++){.
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);.

### Can you use a for each loop to transverse through a 2D array?

We can loop through 2D arrays using nested for loops or nested enhanced for each loops. The outer loop for a 2D array usually traverses the rows, while the inner loop traverses the columns in a single row.

### How to iterate through two

To loop over two dimensional array in Java you can use two for loops. Each loop uses an index. Index of outer for loop refers to the rows, and inner loop refers to the columns. You can then get each element from the array using the combination of row and column indexes.

### How do you solve a 2D array?

Follow the steps below to solve the problem:.
Initialize a 2D difference array D[][], such that D[i][j] stores A[i][j] – A[i][j – 1] (for 0 ≤ i ≤ N and 0 < j < M) or D[i][j] = A[i][j] otherwise..
Traverse each row and compute and store the difference between adjacent elements.. 