【发布时间】:2014-12-25 18:51:48
【问题描述】:
我有一个字符串数组,它没有排序并且有重复的元素。我想计算不同的元素,但是当我调用我的方法时,它返回所有元素的数量,而不仅仅是不同的元素。有什么想法吗?
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;
}
}
【问题讨论】:
标签: java arrays string duplicates distinct