【发布时间】:2013-10-12 05:12:34
【问题描述】:
我遇到了这个问题:我要编写一个方法 contains3,它接受一个字符串列表作为参数,如果任何单个字符串在列表中至少出现 3 次,则返回 true,否则返回 false。我需要使用地图。
当一个词出现三个实例时,仍然不返回true;我无法确定哪里出了问题。
这是我所拥有的:
private static boolean contains3(List<String> thing) {
Map<String, Integer> wordCount = new TreeMap<String, Integer>();
for (String s: thing) {
String word = s;
if (wordCount.containsKey(word)) { // seen before.
int count = wordCount.get(word);
wordCount.put(word, count + 1);
} else {
wordCount.put(word, 1); // never seen before.
}
if (wordCount.containsValue(3)) {
return true;
} else {
return false;
}
}
return false;
}
【问题讨论】:
-
当一个词出现三个实例时,仍然不返回true;我无法确定哪里出了问题。
-
@JackL。你不能使用集合中的本地方法吗?
-
不确定,但是我应该使用地图来解决问题。
-
调试会有所帮助
-
是的......但由于某种原因,我的 Eclipse 在我下载深色 UI 后调试时不显示变量。可能是一个错误;我肯定需要修复我的 Eclipse。