Solution Review: Print a Matrix
In this review, solution of the challenge 'Print a Matrix' from the previous lesson is provided.
We'll cover the following...
Solution
Press + to interact
class TwoDimArr {public static void main( String args[] ) {int n = 3;int[][] arr = new int[n][n];for (int i = 0; i < arr.length; i++) { //assign values to the arrfor (int j = 0; j < arr.length; j++) {if (i == j) { //if row=column=> fill the matrix with 0arr[i][j] = 0;} else if (i > j) { //if row>columns=> fill matrix with -1arr[i][j] = -1;} else { //if row<columns=> fill matrix with 1arr[i][j] = 1;}}}for (int i = 0; i < arr.length; i++) { //print the arrayfor (int j = 0; j < arr.length; j++) {System.out.print(arr[i][j] + " ");}System.out.println();}}}