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

2
  • 1
    Possible duplicate of Java - Best way to print 2D array? and you can use int instead of class Integer Commented Dec 8, 2017 at 15:48
  • Try this, System.out.println(java.util.Arrays.deepToString(x).replace("[","").replace(",","\n").replace("]","").replace(" ","")); Commented Dec 8, 2017 at 19:42

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

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.