所以我在理解此代码方面遇到了一些困难。for-each输入来自数组的字符串以及计数器数量相同的字符串数,但是计数器如何执行此操作?
在以下位置传递给反击的数字是多少: Integer count = map.get(nextString);
if statements是什么?
HashMap<String, Integer> map = new HashMap<>();
for (String nextString : inArray) {
Integer count = map.get(nextString);
if (count == null) {
count = 1;
} else {
count = count + 1;
}
map.put(nextString, count);
}
HashMap<String, Integer> map = new HashMap<String, Integer>();
这只是初始化我们的hashmap,没什么复杂的。
for (String nextString : inArray) {
Integer count = map.get(nextString);
在这里,我们正在寻找与我们的 key 关联的 value (在这种情况下,来自数组的字符串(。
if (count == null) {
count = 1;
因为我们正在使用给定字符串发生的次数更新地图,如果存在与键关联的 no 值,则该字符串尚未计数,因此我们将count
设置为1因为这是我们数组中此字符串的第一次出现。
} else {
count = count + 1;
如果上述if-statement未执行,则意味着与字符串相关联,因此我们可以将其递增,然后将其放回地图中。
}
map.put(nextString, count);
}