Languages

Java - how to print nested array?

0 points
Asked by:
Dirask Community
6300

How can I print nested (2D) array?

I tried Arrays.toString() method but I got the following result:

[[I@e580929, [I@1cd072a9]

My code:

import java.util.Arrays;

public class Example {

    public static void main(String[] args) {
        int[][] numbers = {{1, 2, 3}, {4, 5, 6}};
        System.out.println(Arrays.toString(numbers));
    }
}
1 answer
0 points
Answered by:
Dirask Community
6300

Use Arrays.deepToString instead of Arrays.toString() method.

Practical example:

import java.util.Arrays;

public class Example {

    public static void main(String[] args) {
        int[][] numbers = {{1, 2, 3}, {4, 5, 6}};
        System.out.println(Arrays.deepToString(numbers));
    }
}

output:

[[1, 2, 3], [4, 5, 6]]
0 comments Add comment
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join