我有一个字符串数组,它是未排序的,它有重复的元素。我想统计不同的元素,但是当我调用我的方法时,它会返回所有元素的数量,而不仅仅是不同的元素。有什么想法吗?查找未排序字符串数组的不同元素
public static double countDistinctString(String[] array) {
double distinctStrings = 0;
for (int j = 0; j < array.length; j++){
String thisString = array[j];
boolean seenThisStringBefore = false;
for (int i = 0; i < j; i++){
if (thisString == array[i]){
seenThisStringBefore = true;
}
}
if (!seenThisStringBefore){
distinctStrings++;
}
}
return distinctStrings;
}
}
感谢这么多,问题就解决了:) – Najme