如何在java中的二维字符串数组中找到唯一的元素?

如何在java中的二维字符串数组中找到唯一的元素?,java,algorithm,sorting,Java,Algorithm,Sorting,我有一个二维字符串数组。这是一个矩阵。我需要对该矩阵进行排序,并将唯一项保存在第一行其他矩阵中。如何做到这一点,请仅使用自己的arlgorithm。我的意思是,不要调用方法,而是编写循环本身,以对数组的元素进行排序和比较 import java.util.Scanner; public class Coursework { public static void main(String[] args) { final int linesOfMatrix; //number

我有一个二维字符串数组。这是一个矩阵。我需要对该矩阵进行排序,并将唯一项保存在第一行其他矩阵中。如何做到这一点,请仅使用自己的arlgorithm。我的意思是,不要调用方法,而是编写循环本身,以对数组的元素进行排序和比较

  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]  );
            }

        }
    }
    }
import java.util.Scanner;
公共课作业{
公共静态void main(字符串[]args){
final int linesOfMatrix;//矩阵中的行数
System.out.println(“输入行数:”);
扫描仪sc=新的扫描仪(System.in);
linesOfMatrix=sc.nextInt();
扫描仪sc2=新扫描仪(System.in);
String[][]矩阵=新字符串[linesOfMatrix][];//声明矩阵
对于(int i=0;i
使用带有计数元素的
Map
怎么样:

public static String[] getUnique(String[][] matrix) {
    Map<String, Integer> map = new LinkedHashMap<>();

    for (String[] row : matrix)
        for (String col : row)
            map.put(col, map.getOrDefault(col, 0) + 1);

    List<String> unique = new ArrayList<>();

    for (Map.Entry<String, Integer> entry : map.entrySet())
        if (entry.getValue() == 1)
            unique.add(entry.getKey());

    return unique.toArray(new String[unique.size()]);
}
甚至不要使用
列表
:-):

公共静态字符串[]getUnique(字符串[][]矩阵){
int-total=0;
对于(int row=0;row
你能为这个问题提供一个测试用例吗?@oleg.cherednik不理解这个问题,你需要输入数据吗?@oleg.cherednik这个数据用于输入是的,你能用文字做一些注释吗。读下划线是什么意思?我仍然看不到转换的逻辑。@oleg.cherednik红线没有任何意义。
public static String[] getUnique(String[][] matrix) {
    List<String> unique = new ArrayList<>();

    for (int row = 0; row < matrix.length; row++) {
        for (int col = 0; col < matrix[row].length; col++) {
            if (matrix[row][col] == null)
                continue;

            boolean foundUnique = true;

            for (int i = row; i < matrix.length; i++) {
                for (int j = i == row ? col : 0; j < matrix[i].length; j++) {
                    if (matrix[i][j] == null || (i == row && j == col))
                        continue;

                    if (matrix[i][j].equals(matrix[row][col])) {
                        foundUnique = false;
                        matrix[i][j] = null;
                    }
                }
            }

            if (foundUnique)
                unique.add(matrix[row][col]);
            else
                matrix[row][col] = null;
        }
    }

    return unique.toArray(new String[unique.size()]);
}
public static String[] getUnique(String[][] matrix) {
    int total = 0;

    for (int row = 0; row < matrix.length; row++) {
        for (int col = 0; col < matrix[row].length; col++) {
            if (matrix[row][col] == null)
                continue;

            boolean foundUnique = true;

            for (int i = row; i < matrix.length; i++) {
                for (int j = i == row ? col : 0; j < matrix[i].length; j++) {
                    if (matrix[i][j] == null || (i == row && j == col))
                        continue;

                    if (matrix[i][j].equals(matrix[row][col])) {
                        foundUnique = false;
                        matrix[i][j] = null;
                    }
                }
            }

            if (foundUnique)
                total++;
            else
                matrix[row][col] = null;
        }
    }

    if (total == 0)
        return new String[0];

    String[] res = new String[total];

    for (int row = 0, i = 0; row < matrix.length; row++)
        for (int col = 0; col < matrix[row].length; col++)
            if (matrix[row][col] != null)
                res[i++] = matrix[row][col];

    return res;
}