Skip to content

Commit b5fea96

Browse files
Committing code to print matrix
1 parent 27a6706 commit b5fea96

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.java4u.ds.matrix.print;
2+
3+
import java.util.Arrays;
4+
5+
public class MatrixPrint {
6+
7+
public static void main(String[] args) {
8+
int[][] mat = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
9+
System.out.println("===========================");
10+
print(mat);
11+
System.out.println("===========================");
12+
printUsingForEach(mat);
13+
System.out.println("===========================");
14+
printUsingtoString(mat);
15+
}
16+
17+
private static void print(int[][] mat) {
18+
for (int i = 0; i < mat.length; i++) {
19+
for (int j = 0; j < mat[i].length; j++) {
20+
System.out.print(mat[i][j] + " ");
21+
}
22+
System.out.println();
23+
}
24+
}
25+
26+
private static void printUsingForEach(int[][] mat) {
27+
for (int[] row : mat) {
28+
for (int i : row) {
29+
System.out.print(i + " ");
30+
}
31+
System.out.println();
32+
}
33+
}
34+
35+
private static void printUsingtoString(int[][] mat) {
36+
for (int[] row : mat) {
37+
System.out.println(Arrays.toString(row));
38+
}
39+
}
40+
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
*
3+
*/
4+
/**
5+
* @author varadago
6+
*
7+
*/
8+
package com.java4u.ds.matrix.print;

0 commit comments

Comments
 (0)