2017-11-28 7 views
0

私はStringの2次元配列を持っています。これは行列です。私はこの行列をソートし、最初の行に他のMatrixを保存する必要があります。これは独自のarlgorithmを使って行います。つまり、メソッドを呼び出さずに並べ替えて配列の要素を比較するループ自体を書くことです。Javaの文字列の2次元配列のユニークな要素を見つけるには?

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

     } 
    } 
    } 
+0

を? –

+0

@ oleg.cherednikは質問を理解できませんでした。入力データが必要ですか? – Bogdan

+0

@ oleg.cherednik入力のためのこのデータhttps://drive.google.com/file/d/191y56tw-WKyM20kRxlMcewrLLCf3RQcl/view?usp=sharing – Bogdan

答えて

0

カウント要素とMapの使用についてどのような:、あなたは少しだけ遅くして同じことを行う、あなたが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()]); 
} 

coudl:

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

あるいはList :-)使用しないでください:あなたは、このためのテストケースを提供することができ

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; 
} 
関連する問題