1

I was wondering what the simplest way of printing a 2D array? I know we can print 1D array just like this code below.

int[ ] numbers = {2, 3, 5, 7};    
for (int print: numbers) {
   System.out.println(print); 
}

I tried to print the code below of course it didn't work.

int[ ][ ] numbers= { {1, 2, 3}, {4, 5, 6} }; 
for (int print: numbers) {
       System.out.println(print); 
    }

I know I can print with nested loops like the code below but I was wondering if I can use foreach loop just like 1D array. Thank you

 int[ ][ ] numbers= { {1, 2, 3}, {4, 5, 6} }; 
    for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers[i].length; j++) {
                System.out.print(numbers[i][j] + " ");
            }
            System.out.println();
        }
}
0

4 Answers 4

2

In 2-dimensional array each element in first loop will be an 1-dimensional array, in second loop each element is a number itself:

int[][] array2d = {{1, 2, 3}, {4, 5, 6}};
for (int[] array : array2d) {
    for (int element : array) {
        System.out.print(element + " ");
    }
    System.out.println();
}

Actually, there is a better way using Arrays::toString library method:

int[][] numbers = {{1, 2, 3}, {4, 5, 6}};
for (int[] array : numbers) {
    System.out.println(Arrays.toString(array));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @J-Alex! It is what I have been trying to do last 45 minutes :)
2

print 2D array simplest way with foreach loop :

Exmple :

 public class Print2DArr {


            public static void main(String[] args) {
                // TODO Auto-generated method stub

                         int[ ][ ] numbers= { {1, 2, 3}, {4, 5, 6} }; 
                              for (int[] arr:  numbers) {
                              for (int num :  arr ) {
                              System.out.print(num +  " ");
                    }
                    System.out.println();
                }
             }

          }

Output :

1 2 3 
4 5 6 

However, the libs do supply the method deepToString for this purpose, so this may also suit your purposes:

public class Print2DArr {


    public static void main(String[] args) {
        // TODO Auto-generated method stub

                 int[ ][ ] numbers= { {1, 2, 3}, {4, 5, 6} }; 
                   System.out.println(Arrays.deepToString(numbers));

}

Output :

[[1, 2, 3], [4, 5, 6]]

1 Comment

Try my second purposes!
1

You can use Stream.of() for that too:

Stream.of(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}).forEach(x -> {
        System.out.println(Arrays.toString(x));
    });
Output: [1, 2, 3]
        [4, 5, 6]
        [7, 8, 9]

But Arrays.deepToString() is a better way I think.

System.out.println(
        Arrays.deepToString(
            new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
        ));
Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Comments

0

Here is my attempt to use a foreach loop to print a 2D array:

public class MyClass
{

    public static void main(String[] args)
    {
        int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };

        // To access the elements of the myNumbers array,
        // specify two indexes: one for the array, and one
        // for the elements inside that array.

        for(int i : myNumbers[0]) // Here accessing the first array with index 0:
        {
            System.out.println(i);
        }
        for(int j : myNumbers[1]) // Here accessing the second array with index 1:
        {
            System.out.println(j);
        }
    }
}

Output: 1 2 3 4 5 6 7

1 Comment

Compared to the other answers here, yours requires knowing ahead of time that you'll have exactly two arrays nested within myNumbers. If you were to add another one, it won't get printed, and if you removed one (or both) of the nested arrays, the code would crash.

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.