[英]How to find unique elements in a two-dimensional array of strings in java?
I have a two-dimensional array of String. 我有一个二维的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] );
}
}
}
}
What about using Map
with counting elements: 如何将
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()]);
}
In case you do not want to use Map
, then you coudl just do the same with a bit slower: 如果您不想使用
Map
,那么您可以以较慢的速度进行操作:
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()]);
}
Or even do not use List
:-): 甚至不使用
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.