How to print a matrix with outer elements sorted and a blank in the place of inner elements in JAVA? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to print a matrix with outer elements sorted and a blank in the place of inner elements in JAVA?

Eg 9 8 2 2 0 9 -2 7 2 9 1 0 output- 0 0 1 2 9 2 9 8 7 2

13th May 2018, 6:40 AM
Ansh Lalchandani
1 Answer
+ 1
The matrix will be mat[i][j]. First of all we sort the matrix, with simple for we get all the integers and place them. To clean out the middle we need to check that: 1+1 < width && height if so, we create a string matrix, to handle empty values in the middle. String[][] newMat = new String[mat.length][] for (int i=0; i <newMat.length; i++) for (int j=0; j < newMat [i].length; i++) newMat [i][j] = mat [i][j].toString(); //Clear middle for (int i = 1; i <newMat.length - 1; i++) for (int j = 1; j < newMat [i].length-1; i++) newMat [i][j] = "";
13th May 2018, 9:58 AM
Eldar Bakerman