Consider it as an array of arrays and this will work for sure.

int mat[][] = { {10, 20, 30, 40, 50, 60, 70, 80, 90},
                {15, 25, 35, 45},
                {27, 29, 37, 48},
                {32, 33, 39, 50, 51, 89},
              };


    for(int i=0; i<mat.length; i++) {
        for(int j=0; j<mat[i].length; j++) {
            System.out.println("Values at arr["+i+"]["+j+"] is "+mat[i][j]);
        }
    }
Answer from Bharat on Stack Overflow
Discussions

java - loops in a two-dimensional array - Stack Overflow
I was wondering if there is a way to manipulate two variables in a for loop. Also, how could I make it so that the first row will increase one cell when the cells within the row is done in a loop (in this case cell 0 through 4 in one row) And is there a way to output a specific range of cells? thanks and sorry I know this might be pretty confusing ... Brute forcing as in array ... More on stackoverflow.com
🌐 stackoverflow.com
java - For each loop using 2D array - Stack Overflow
The loop is iterating on the elements of uu, which are objects of type int[]. (Or in other words - u is an element in uu, thus it is an int[]). The declaration is always of the type of the objects retrieved by the iteration - in this case - it is int[] - ... The array is two-dimensional, so you ... More on stackoverflow.com
🌐 stackoverflow.com
java - Dont understand for each loop with two dimensional array - Stack Overflow
Why we had to make this loop ? : for(int y:x) Thats the part which confuse me a lot, i know how to do basic for each loop with basic array, but this confuse me a lot when i use two dimensional array.. More on stackoverflow.com
🌐 stackoverflow.com
Most common term in a two dimensional array
The way you have described it, the 2d array bit seems irrelevant to the general solution. You have a data structure that contains values and you want to find the most common value. How would you solve that in general? More on reddit.com
🌐 r/learnprogramming
64
20
August 25, 2024
🌐
Codecademy
codecademy.com › learn › apcs-arrays-and-loops › modules › apcs-two-dimensional-arrays › cheatsheet
Arrays and Loops: Two-Dimensional Arrays Cheatsheet | Codecademy
... In Java, 2D arrays are stored as arrays of arrays. Therefore, the way 2D arrays are declared is similar 1D array objects. 2D arrays are declared by defining a data type followed by two sets of square brackets.
🌐
Blogger
javarevisited.blogspot.com › 2015 › 09 › how-to-loop-two-dimensional-array-in-java.html
How to loop over two dimensional array in Java? Example
June 27, 2025 - You can loop over a two-dimensional array in Java by using two for loops, also known as nested loop. Similarly to loop an n-dimensional array you need n loops nested into each other.
🌐
Coding Rooms
codingrooms.com › blog › iterating-through-2d-array-java
Iterating Through 2D Array Java
October 5, 2020 - package exlcode; public class ... System.out.println(exampleVariableOne[countOne][countTwo]); } } } } The first for loop loops through each row of the 2D array one by one....
🌐
Devcubicle
devcubicle.com › two-dimensional-array-for-loop-java
How to use for loop with two dimensional array in Java - DevCubicle By Cloud Tech
December 29, 2019 - 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.
🌐
Codecademy
codecademy.com › learn › learn-java › modules › java-two-dimensional-arrays › cheatsheet
Learn Java: Two-Dimensional Arrays Cheatsheet | Codecademy
... In Java, 2D arrays are stored as arrays of arrays. Therefore, the way 2D arrays are declared is similar 1D array objects. 2D arrays are declared by defining a data type followed by two sets of square brackets.
Find elsewhere
🌐
Java67
java67.com › 2014 › 10 › how-to-create-and-initialize-two-dimensional-array-java-example.html
How to declare and Initialize two dimensional Array in Java with Example | Java67
You can see my post 6 ways to declare two dimensional array in Java for more details.Delete ... Hello @Anonymous, you can do this by using a 2 dimensional for loop, first loop will fill the rows and second will fill the columns.
🌐
Vertex Academy
vertex-academy.com › tutorials › en › two-dimensional-arrays-java
Two-Dimensional Arrays in Java • Vertex Academy
March 28, 2018 - In order to display a value in each cell of a two-dimensional array, it's not enough to use a single "for" loop; instead, it's necessary to use two "for" loops, and one of them has to be located inside the other.
🌐
Tutorialspoint
tutorialspoint.com › java › java_multi_dimensional_arrays.htm
Java Multi-Dimensional Arrays
In Java, we can initialize the values of a two-dimensional array using nested for loops, in which the first loop is used to iterate over the rows and the second is used to iterate over the columns.
🌐
Tutorial Gateway
tutorialgateway.org › two-dimensional-array-in-java
Two Dimensional Array in Java
March 23, 2025 - Let us see the Java two dimensional array program execution in iteration wise ... The value of the row will be 0, and the condition (rows < 2) is True. So, it will enter into second for loop.
🌐
DEV Community
dev.to › realedwintorres › accessing-elements-in-2d-arrays-in-java-1o7i
Accessing Elements in 2D Arrays in Java - DEV Community
November 28, 2021 - 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} 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:
🌐
GeeksforGeeks
geeksforgeeks.org › java › print-2-d-array-matrix-java
Print a 2D Array or Matrix in Java - GeeksforGeeks
March 24, 2025 - // Java program to print the elements of // a 2 D array or matrix using toString() import java.io.*; import java.util.*; // Driver Class class Geeks { public static void print2D(int mat[][]) { // Loop through all rows for (int[] row : mat) // converting each row as string // and then printing in a separate line System.out.println(Arrays.toString(row)); } public static void main(String args[]) throws IOException { int mat[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; print2D(mat); } }
Top answer
1 of 6
6

Ok, so consider this code:

for(int x[] : numbers) {
    for(int y : x) {
        System.out.println("Number is: " + y);
    }
}

Now, on the first line, you see some odd syntax. The int x[] is saying the same things as int[] x. (All it is, is a different way of saying the same thing.) Basically, you're declaring an array. Now the foreach loop takes each element in the array you specify after the :, or numbers, and places it into the variable you declared before the :, or x.

Now, the best way to understand this code is to understand how 2D arrays work. They aren't some new special thing, all a 2D array is, is an array of arrays. Instead of each element being a value, like in a traditional array, each element is a whole other array, with it's own set of values. So let's look at the top line of code again.

for(int x[] : numbers) {

This, in english, translates to, "For each of the elements in numbers, place it's value in x and run the code below." Remember that because numbers is an array of arrays, each of it's elements is an array. So the code runs through the first iteration, and retrieves the first element in numbers (at index 0), which is an array of ints. Then, it runs the code below:

for(int y : x) {
    System.out.println("Number is: " + y);
}

The top line of code can be translated to english as follows: "For each of the elements in x, place it's value in y and run the code below." So, it does just that. The code runs through the first iteration and retrieves the first element in x, which is an int. Then, it runs the code:

System.out.println("Number is: " + y);

Of course, that prints out the value it retrieved. Then, the inner for loop keeps going through the array x until it has reached the end. At this point, just like any other for loop, it exits.

Now, remember that you are still only on the first iteration of the for(int x[] : numbers) loop. So now that all the code inside has run, it retrieves the next element in numbers, or the next array. (Because numbers is an array of arrays) It runs the code below again, repeating until it has run the code for every element (or array) in numbers.

Hope that helps. Feel free to ask me questions in the comments.

Edit:

For your questions in the comments, let's look at this graphically.

So the orange box here is numbers. It is an array. Each of it's elements in one of the blue boxes. (For a total of 3). Now, look at the top blue box. It is an array. The top blue box is numbers[0], or, (during the first iteration of the outer loop) x. Now look at the inside of the top blue box, at the green boxes. Each of the blue box's elements is represented by a green box. Each green box is an int. The first green box in the first blue box is (during the first iteration of the outer loop), x[0], or (during the first iteration of both the outer and inner loops), y. So the outer loop is looping through all of the blue boxes (x) in the orange box (numbers), and the inner loop is looping through all the green boxes (y) in the blue box (x) that it was given.

Does that make more sense?

2 of 6
6

First, we are going to rename numbers to grid, x to row, and y to cell. Let's break it down.

for (int x[]:numbers) can be rewritten as for (int[] row : grid). This iterates over all int[]s stored in grid. For each int[], row is set to that int[].

The inner loop, for (int y : x), rewritten as for (int cell : row) is iterating over all of the values of row, which is an int[], and setting cell equal to each value. The first time the outer then inner loops run, row is equal to grid[0] and cell is equal to row[0], or grid[0][0]. This continues until every value in grid has been used.

🌐
Medium
medium.com › @AlexanderObregon › how-javas-multi-dimensional-arrays-work-for-beginners-faf4f4a9935d
How Java’s Multi-Dimensional Arrays Work for Beginners
February 24, 2025 - Some looping methods are more efficient than others because of how Java handles memory access behind the scenes. A traditional way to iterate through a two-dimensional array is by using a nested for loop.
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit8-2DArray › topic-8-2-2D-array-loops-Day1.html
8.2.1. Nested Loops for 2D Arrays (Day 1) — CSAwesome v1
Since you can find out the number of rows and columns in a 2D array you can use a nested for loop (one loop inside of another loop) to loop/traverse through all of the elements of a 2D array.
🌐
Learn
learn.java › learning › tutorials › CS14Lists › iterate-over-2darray
Iterating over a Two-Dimensional Array - Learn.java
Learn how to use nested for loops to iterate over the elements of a two-dimensional array
🌐
CodeGym
codegym.cc › java blog › java arrays › 2d arrays in java
Java Matrix - 2D Arrays
April 8, 2025 - In the first iteration, String[]row will read “Hi, I am Karen” as an Array, convert it to a String and then print it. That’s how all iterations will take place. The utility provided here is that you don’t have to keep track of any indexes (i, j) or nested loops. 2D Arrays in Java are the most simpler of the multi-dimensional arrays.