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 OverflowConsider 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]);
}
}
Just invert the indexes' order like this:
for (int j = 0; j<array[0].length; j++){
for (int i = 0; i<array.length; i++){
because all rows has same amount of columns you can use this condition j < array[0].lengt in first for condition due to the fact you are iterating over a matrix
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]);
}
}
You may want something like that :
Preferences [0][0]="Tom";
Preferences [0][1]="Coke";
Preferences [1][0]="John";
Preferences [1][1]="Pepsi";
You'll know that Preferences[0] is about Tom
You'll know that Preferences[1] is about John
And once you have it, the columns will be [0]=>"name" [1] =>"drink"
[0][1] will give you Tom[0] s drink[1] [Coke] for example.
[0][0] will give you Tom[0] s name[0] [Tom] for example.
[1][1] will give you John[1] s drink[1] [Pepsi] for example.
[1][0] will give you John[1] s name[0] [John] for example.
java - loops in a two-dimensional array - Stack Overflow
java - For each loop using 2D array - Stack Overflow
java - Dont understand for each loop with two dimensional array - Stack Overflow
Most common term in a two dimensional array
Videos
Since your uu is an array of array. So, when you iterate over it, you will first get an array, and then you can iterate over that array to get individual elements.
So, your outer loop has int[] as type, and hence that declaration. If you iterate through your u in one more inner loop, you will get the type int: -
for (int[] u: uu) {
for (int elem: u) {
// Your individual element
}
}
It is because uu is an array of int[] arrays. So every item in it is int[]. In a for loop you declare the type of an item in an array you iterate over.
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?
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.