17

This is the snippet of Java code:

int[][] uu = new int[1][1];
uu[0][0] = 5;
for(int[] u: uu){
    System.out.println(u[0]);
}

It prints 5. But why does the declaration part of for loop is declared as int[] u, but not as int[][] u?

At the uu you reference 2D array... That is not a homework. I am preparing for Java certification. Cheers

4
  • Just a silly suggestion: - Its not 2-D array in Java. Its an array of array. Commented Nov 14, 2012 at 17:15
  • It is implemented in the way of array in array, but it is called 2D array in books. Commented Nov 14, 2012 at 17:17
  • @uml.. May be, but still an array of array is a better term to name it. You will know it in future. :) Commented Nov 14, 2012 at 17:20
  • @uml I know a lot of books call it a 2D array, and if I needed a 2D array in a Java program I would map it to this structure, but it really is an array of arrays, and it is much better to think of it that way. For example, the answer to your question becomes obvious if you think of the for loop as iterating over an array, each of whose elements is an int[]. Commented Nov 14, 2012 at 17:22

5 Answers 5

40

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2

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[] -

Same as iterating over an int[] is:

 for (int x : myArray) { ...}

because each element of x is of type int.

Comments

0

"why does the declaration part of for loop is declared as int[] u, but not as int[][] u?"

The array is two-dimensional, so you are dealing with a double-layered iteration. You have an array "inside" another, in the same principle as List<List<Integer>> would work.

To iterate through all the elements, you should consider a rows-elements structure. It's necessary that you get each row from the container, and then each element from each row.

for(int[] u: uu) is simply a for-each iteration rows, with the same principle of for(int row = 0; row < container.length; row++), and u or respectively container[row] are not elements themselves, but rows (arrays) of elements. Meaning you require a second iteration layer to get the elements:

int[][] container = new int[10][10];
//... - Fill elements.
for(int row = 0; row < container.length; row++){
  for(int element = 0; element < container[row].length; element++){
    System.out.printf("Row: %d Element: %d Value: %d\n", row, element, container[row][element]);
  }
}

Comments

-1

This is an example of finding the sum in 2d array and print it

public class Array2DForEach {
    public static void main(String[] args) {
        int sum = 0;
        int[][] myFirst2DArray = {
                                    { 3, 5, 1, 9 },
                                    { 10, 15, 3, 0  },
                                    { 1, 11, 31, 90 },
                                    { 2, 51, 1, 9 }
                                };

        for (int[] row:myFirst2DArray) {

            for (int columnElement : row) {
                sum+=columnElement;
            }
            
        }
        System.out.println(sum);
    }
}

1 Comment

This answer does not answers the asked question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.