Try this, it's the correct way to traverse a two-dimensional array of arbitrary size:

for (int i = 0; i < Preferences.length; i++) {
    for (int j = 0; j < Preferences[i].length; j++) {
        System.out.print(Preferences[i][j]);
    }
}
Answer from Óscar López on Stack Overflow
🌐
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.
🌐
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.
🌐
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.
🌐
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.
🌐
Vertex Academy
vertex-academy.com › tutorials › en › two-dimensional-arrays-java
Two-Dimensional Arrays in Java • Vertex Academy
March 28, 2018 - The second "for" loop checks each element of each column in each row. By inserting one "for" loop inside another, you can assign values to elements of a two-dimensional array. ... You can find more articles in our Java Tutorial for Beginners.
🌐
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.
Find elsewhere
🌐
Tutorial Gateway
tutorialgateway.org › two-dimensional-array-in-java
Two Dimensional Array in Java
April 5, 2016 - TIP: In order to store the elements in a two-dimensional array, we can use a for loop, a Java While Loop, and a Java Do While 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:
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
adilwadhwania.medium.com › two-dimensional-array-in-java-97c0bd408032
Two dimensional array in java. Understand what is two dimensional… | by Adil | Medium
May 9, 2023 - So here’s how it works, I hope you all are familiar with for loop. The outer loop will start from 0 and will iterate till “i” is less than the length of our declared array, “twoDArray.length” will return the no of rows in 2D array and in our case it is 3, so the outer loop will iterate 3 times.
🌐
Stack Overflow
stackoverflow.com › questions › 29504671 › for-loop-to-print-two-dimensinal-array
java - For loop to print two dimensinal array - Stack Overflow
Hi Am trying to print 2 dimensional array using for loop. Many places I see nested for loop like ... But how to print using for loop in declaration and expression . for single dimensional following works · for ( String print_fruit_names : fruit_names ) { sysout (names); }; Am a newbie in java. please let me know if am missing any info to mention. ... String[][] matrix = {{"one", "two", "three"}, {"four", "five", "six"}}; for (String[] sArray: matrix){ for (String s: sArray){ System.out.println(s); } }
🌐
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.
🌐
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-Day2.html
8.2.5. Enhanced For-Each Loop for 2D Arrays (Day 2) — CSAwesome v1
Picture pict = new Picture("beach.jpg"); // A 2D array of pixels Pixel[][] pixels = pict.getPixels2D(); Pixel p = pixels[0][0]; // get the first pixel int blue = p.getBlue(); // get its blue value System.out.println("Pixel (0,0) has a blue value of " + blue ); p.setBlue(255); // set its blue value to 255 · You can loop through all the Pixel objects in the two-dimensional array to modify the picture.
🌐
GeeksforGeeks
geeksforgeeks.org › java › print-2-d-array-matrix-java
Print a 2D Array or Matrix in Java - GeeksforGeeks
1 month ago - The inner for-each loop gets each element from the current row. System.out.println() moves the output to the next line after each row. The Arrays.toString() method can be used to convert each one-dimensional row into a readable string.