About Multidimensional Arrays (solved) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 14

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 } }

12th Dec 2020, 11:25 AM
Aysha
Aysha - avatar
12 Answers
+ 7
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
12th Dec 2020, 12:49 PM
Aysha
Aysha - avatar
+ 6
Mohammed Qadir khan thanks you much for solving my doubt.
12th Dec 2020, 2:00 PM
Aysha
Aysha - avatar
+ 5
Flash I just gave the example. See I have changed the output elements which I need as I have asked in the question.
12th Dec 2020, 1:07 PM
Aysha
Aysha - avatar
+ 3
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
12th Dec 2020, 12:06 PM
Flash
+ 3
I think this lesson is what u r asking for : https://www.sololearn.com/learn/Java/2149/?ref=app
12th Dec 2020, 12:38 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 3
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]
12th Dec 2020, 12:47 PM
Flash
+ 3
Most Welcome 🙂, Ok No Problem.
12th Dec 2020, 2:04 PM
Mohammed Qadir khan
Mohammed Qadir khan - avatar
+ 2
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
12th Dec 2020, 11:42 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 2
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) {}
12th Dec 2020, 12:53 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
12th Dec 2020, 12:59 PM
Flash
0
Aysha Simra where did get 2? what kind of order is that?
12th Dec 2020, 12:56 PM
Flash