About Multidimensional Arrays (solved)
Please somebody explain me how to print multidimensional array elements in a new line with Explaination. It is really confusing how to print the elements in a new line. I tried a lot but failled to execute it. So please help me. import java.util.Arrays; public class Main { public static void main(String[] args) { int[][] matrix = new int[][]{ {8, 1, 6}, {3, 5, 7}, {4, 9, 0}, }; System.out.println(Arrays.deepToString(matrix)); //output the numbers } }
12/12/2020 11:25:33 AM
_Aysha_
13 Answers
New AnswerTaken idea from: https://www.google.com/amp/s/www.geeksforgeeks.org/flatten-a-stream-of-arrays-in-java-using-foreach-loop/amp/ https://code.sololearn.com/c41Xv7j2d2X5/?ref=app
Yesss Alphin K Sajan i am confused on this topic wheter to take loop aur to use any other way to solve this I wanted output in this way 8 1 6 3 5 7 4 9 0
Flash I just gave the example. See I have changed the output elements which I need as I have asked in the question.
it's a bit unclear. Do you mean something like this? public class Main { public static void main(String[] args) { int[][] a = { {1, 2, 3}, {4, 5, 6} }; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { System.out.printf("%d ", a[i][j]); } System.out.println(); } } } out: 1 2 3 4 5 6
I think this lesson is what u r asking for : https://www.sololearn.com/learn/Java/2149/?ref=app
in Java11+ you can try this as well import java.util.Arrays; public class Main { public static void main(String[] args) { int[][] matrix = { {8, 1, 6}, {3, 5, 7}, {4, 9, 0}, }; for (var i : matrix) { System.out.println(Arrays.toString(i)); } } } out; [8, 1, 6] [3, 5, 7] [4, 9, 0]
If you use System.out.println() it'll print a newline characters at end of string. If you have already tried to solve this please share your code here so others can see what's wrong in it. If you don't know how to share code read this: https://www.sololearn.com/post/75089/?ref=app
Flash, Starting from Java 10, actually. https://developer.oracle.com/java/jdk-10-local-variable-type-inference.html For lower versions of Java that var can be replaced by int[] for(int[] a: matrix) {}