[Solved] Create two dimensional array with values dynamically in Java


If you want to print the second matrix you show on the post then you must assign those values to the corresponding cells of the matrix you have created before printing the matrix, because doing

int matrix[][] = new int[n][n];

will create a matrix of dimension NxN but filled in every cell with a 0

I don’t know the logic to fill your matrix (reading from user input or generated by some algorithm) but for every row and column that you want to fill with a value different from 0 you need to do an assignment:

matrix[row][col] = someInt;

solved Create two dimensional array with values dynamically in Java