Java 在数组中查找唯一字符串
我试图搜索一个数组,找出所有唯一的字符串,这意味着每个字符串只打印一个实例。然后我想把它们打印出来。但是,它没有检测到任何唯一的字符串。有人能帮我弄清楚是怎么回事吗 注意:本质上,我试图计算有多少字符串,而不计算重复的字符串Java 在数组中查找唯一字符串,java,string,unique,Java,String,Unique,我试图搜索一个数组,找出所有唯一的字符串,这意味着每个字符串只打印一个实例。然后我想把它们打印出来。但是,它没有检测到任何唯一的字符串。有人能帮我弄清楚是怎么回事吗 注意:本质上,我试图计算有多少字符串,而不计算重复的字符串 public class Test { public static String uniquecenter; public static void main (String[] args){ //Grab Data and Feed into Centers
public class Test {
public static String uniquecenter;
public static void main (String[] args){
//Grab Data and Feed into Centers
String centers[]=new String[1000];
/* Only for Testing Purposes*/
centers[0] = "Table Tennis";
centers[1] = "Soccer";
centers[2]= "Baseball";
centers[3] = "Table Tennis";
//Unique Centers
String uniquecenters[]=new String [1000];
//0 out array
for(int i=0; i<1000;i++){
uniquecenters[i]="0";
}
//loop through center array
for(int i=0; i <1000; i++){
boolean unique=false;
//Loop through unique centers
for(int l=0; l<1000; l++){
//if it finds that this point in the centers array already has been indexed in the unique centers
//array then it is not unique...so move on to the next center
if(uniquecenters[l].equals(centers[i])){
unique=true;
}
}
//if it never found this string in the uniquecenters array...
if(unique==false){
//find an empty spot in the unique array
for(int l=0;l<1000;l++){
//grab the unique centers string
String c=uniquecenters[l];
//check if something is already in this spot...if not then...
if(uniquecenters.equals("0")){
//..make it a unique center
uniquecenters[l]=uniquecenter;
}//End of placing center
}//end of looking for unique spot
}//end of if it is unique
}//end of creating this unique center array
//print all unique strings
for(int i =990; i>=0;i--){
String print =uniquecenters[i];
//if it is emptry
if(print.equals("0")){
}else{
//print this unique center
System.out.println(print);
}
}
}
公共类测试{
公共静态字符串唯一中心;
公共静态void main(字符串[]args){
//抓取数据并输入中心
字符串中心[]=新字符串[1000];
/*仅用于测试目的*/
中锋[0]=“乒乓球”;
中锋[1]=“足球”;
中锋[2]=“棒球”;
中锋[3]=“乒乓球”;
//独特中心
字符串唯一中心[]=新字符串[1000];
//0输出阵列
对于(int i=0;i,您可以使用根据定义不包含重复项的集合
:
String centers[];
...
List<String> centerList = Arrays.asList(centers);
Set<String> uniqueCenters = new HashSet<String>();
uniqueCenters.addAll(centerList);
uniqueCenters.remove(null);
Integer numberOfUniqueStrings = uniqueCenters.size();
字符串中心[];
...
列表中心列表=数组.asList(中心);
Set uniqueCenters=new HashSet();
uniqueCenters.addAll(中心列表);
唯一中心。删除(空);
整数numberOfUniqueStrings=uniqueCenters.size();
您可以使用根据定义不包含重复项的集合:
String centers[];
...
List<String> centerList = Arrays.asList(centers);
Set<String> uniqueCenters = new HashSet<String>();
uniqueCenters.addAll(centerList);
uniqueCenters.remove(null);
Integer numberOfUniqueStrings = uniqueCenters.size();
字符串中心[];
...
列表中心列表=数组.asList(中心);
Set uniqueCenters=new HashSet();
uniqueCenters.addAll(中心列表);
唯一中心。删除(空);
整数numberOfUniqueStrings=uniqueCenters.size();
您可以使用根据定义不包含重复项的集合:
String centers[];
...
List<String> centerList = Arrays.asList(centers);
Set<String> uniqueCenters = new HashSet<String>();
uniqueCenters.addAll(centerList);
uniqueCenters.remove(null);
Integer numberOfUniqueStrings = uniqueCenters.size();
字符串中心[];
...
列表中心列表=数组.asList(中心);
Set uniqueCenters=new HashSet();
uniqueCenters.addAll(中心列表);
唯一中心。删除(空);
整数numberOfUniqueStrings=uniqueCenters.size();
您可以使用根据定义不包含重复项的集合:
String centers[];
...
List<String> centerList = Arrays.asList(centers);
Set<String> uniqueCenters = new HashSet<String>();
uniqueCenters.addAll(centerList);
uniqueCenters.remove(null);
Integer numberOfUniqueStrings = uniqueCenters.size();
字符串中心[];
...
列表中心列表=数组.asList(中心);
Set uniqueCenters=new HashSet();
uniqueCenters.addAll(中心列表);
唯一中心。删除(空);
整数numberOfUniqueStrings=uniqueCenters.size();
为什么不直接使用字符串作为HashMap
的键呢?如何使用HashMap找到唯一的字符串?@antoh这实际上更适合集
。这段代码有很多问题,但从风格上来说,我立即注意到单词unique的奇怪用法,以及使用硬编码限制而不是ode>array.length
。创建一个类型为
的HashMap。使用字符串作为键在数组中循环,并在HashMap中增加该元素的值。然后,遍历HashMap,值为1的键是唯一的。@chrylis集合将删除第二个(和第三个,等等)一个字符串的副本,但不必说它是否唯一。或者你有一种我没有想到的方法。而且,老实说,我甚至没有看过代码,所以我没有看过任何其他编程或逻辑错误。编辑给定OP的编辑,是的,集可能会更好。为什么不直接使用字符串作为HashMap
的键呢?如何使用hashmap找到唯一的字符串?@antoh这实际上更适合集。此代码存在许多问题,但从风格上看,我立即注意到单词unique的奇怪用法以及硬编码限制的使用,而不是数组。length
。创建
类型的hashmap。循环使用字符串作为键,对数组进行粗略处理,并在HashMap中增加该元素的值。然后,遍历HashMap,值为1的键是唯一的。@chrylis set将删除第二个(和第三个,以此类推)一个字符串的副本,但不必说它是否唯一。或者你有一种我没有想到的方法。而且,老实说,我甚至没有看过代码,所以我没有看过任何其他编程或逻辑错误。编辑给定OP的编辑,是的,集可能会更好。为什么不直接使用字符串作为HashMap
的键呢?如何使用hashmap找到唯一的字符串?@antoh这实际上更适合集。此代码存在许多问题,但从风格上看,我立即注意到单词unique的奇怪用法以及硬编码限制的使用,而不是数组。length
。创建
类型的hashmap。循环使用字符串作为键,对数组进行粗略处理,并在HashMap中增加该元素的值。然后,遍历HashMap,值为1的键是唯一的。@chrylis set将删除第二个(和第三个,以此类推)一个字符串的副本,但不必说它是否唯一。或者你有一种我没有想到的方法。而且,老实说,我甚至没有看过代码,所以我没有看过任何其他编程或逻辑错误。编辑给定OP的编辑,是的,集可能会更好。为什么不直接使用字符串作为HashMap
的键呢?如何使用hashmap找到唯一的字符串?@antoh这实际上更适合集。此代码存在许多问题,但从风格上看,我立即注意到单词unique的奇怪用法以及硬编码限制的使用,而不是数组。length
。创建
类型的hashmap。循环使用字符串作为键,对数组进行粗略处理,并在HashMap中增加该元素的值。然后,遍历HashMap,值为1的键是唯一的。@chrylis set将删除第二个(和第三个,以此类推)字符串的副本,但不必说它是否唯一。或者你有一种我没有想到的方法。另外,我老实说,我甚至没有查看代码,所以我没有查看任何其他编程或逻辑错误。编辑给定OP的编辑,是的,设置可能会更好。此代码很可能会添加意外的null
他设置了。此代码很可能会将意外的null
添加到集合中