I have a two-dimensional array of String. This is a Matrix. I need to sort this Matrix and save unique items in first line other Matrix.How to do this use only own arlgorithm.I mean do not call a method but write the loop itself that will sort through and compare the elements of the array
import java.util.Scanner;
public class Coursework {
public static void main(String[] args) {
final int linesOfMatrix; //number of lines in the matrix
System.out.println("Enter number of lines: ");
Scanner sc = new Scanner(System.in);
linesOfMatrix = sc.nextInt();
Scanner sc2 = new Scanner(System.in);
String [][] matrix = new String [linesOfMatrix][]; // declare the Matrix
for(int i=0; i < matrix.length; i++) {
System.out.println("Enter a value for the string " + (i+1) + "
through a space");
matrix[i] = sc2.nextLine().split(" ");
}
sc.close();
sc2.close();
//below must be unique sort, but he dosen't work rigth
for(int i=0; i < matrix.length; i++){
for(int j=0; j < matrix[i].length-1; j++){
if(matrix[i][j].equals(matrix[i][j+1])){
matrix[i][j+1] = matrix[i][j+1];
}
}
}
System.out.println("Matrix");
for(int i=0; i < matrix.length; i++){
for(int j=0; j < matrix[i].length-1; j++){
System.out.println("[" +(i) + "][" + (j) + "]= " + matrix[i]
[j] + " [" + (i) + "][" + (j+1) + "]= " + matrix[i][j+1] );
}
}
}
}