【发布时间】:2021-03-20 04:49:12
【问题描述】:
我正在尝试 sysout 数组中哈希表值的出现次数
我正在使用
int count;
int len = array.length;
for (Map.Entry<String, String> entry : keyword.hashtable().entrySet()) {
count = 1;
for (int i = 0; i < len; i++) {
t = array[i];
if (entry.getValue().equals(t)) {
System.out.println(" " + entry.getValue() + ": " + count);
count++;
}
}
}
但是输出看起来是这样的
only: 1
only: 2
was: 1
was: 2
was: 3
was: 4
it: 1
in: 1
in: 2
in: 3
the: 1
the: 2
the: 3
the: 4
the: 5
the: 6
the: 7
我怎样才能用出现次数只用一次这个词?
这样
only: 2
was: 4
in: 3
the: 7
【问题讨论】:
-
它正在为您计算的每一项打印,因为您告诉它为您计算的每一项打印。仅在执行计数的循环完成后打印计数。