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 } }
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
+ 6
Mohammed Qadir khan thanks you much for solving my doubt.
+ 5
Flash I just gave the example. See I have changed the output elements which I need as I have asked in the question.
+ 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
+ 3
I think this lesson is what u r asking for :
https://www.sololearn.com/learn/Java/2149/?ref=app
+ 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]
+ 3
Most Welcome 🙂, Ok No Problem.
+ 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
+ 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) {}
+ 1
🇮🇳Omkar🕉 thanks
0
Aysha Simra where did get 2? what kind of order is that?