2
Integer [][] x = {{1,2,3},{4},{5,6}};

What is the best way to print the elements in stdout like this?

1
2
3
4
5
6

I have tried Arrays.deepToString(x) but doesnt output what I desire

Pedro Martins
  • 158
  • 2
  • 9
  • 1
    Possible duplicate of [Java - Best way to print 2D array?](https://stackoverflow.com/questions/19648240/java-best-way-to-print-2d-array) and you can use `int` instead of class `Integer` – Pavneet_Singh Dec 08 '17 at 15:48
  • Try this, `System.out.println(java.util.Arrays.deepToString(x).replace("[","").replace(",","\n").replace("]","").replace(" ","")); ` – RAHUL ROY Dec 08 '17 at 19:42

1 Answers1

2

Try this :

public class MyClass {
    public static void main(String args[]) {

        Integer [][] x = {{1,2,3},{4},{5,6}};

        for(Integer[] y : x) {

            for(Integer i : y) {

                System.out.println(i);

            }
        }

    }
}

Output :

1
2
3
4
5
6

Here it is : https://ideone.com/9ywcjz

Mouad EL Fakir
  • 3,609
  • 2
  • 23
  • 37