为什么这个for循环执行“else”代码块?
我想我明白了第一个说法。这就是说,如果“单词”中的项目不在“频率”中,则添加它们并为其赋值1,对不对? 我更困惑的是为什么“else”块被执行以及它是如何工作的。我理解输出,但并不完全如此。它显然认识到一个特定的词出现不止一次,但它是如何认识到这一点的?再说一遍,为什么如果第一个陈述是真的,它会进入“其他”区块?为什么这个for循环执行“else”代码块?
public class Testing {
static List<String> list() {
List<String> words = new ArrayList<String>();
words.add("Cherry");
words.add("Banana");
words.add("Apple");
words.add("Banana");
words.add("Berry");
return words;
}
static Map<String, Integer> ArrayFrequencies(List<String> words) {
Map<String, Integer> frequencies = new HashMap<String, Integer>();
for (String elements : words) {
if (!frequencies.containsKey(elements)) {
frequencies.put(elements, 1);
} else {
frequencies.put(elements, frequencies.get(elements) + 1);
}
}
return frequencies;
}
public static void main(String[] args) {
System.out.println(ArrayFrequencies(list()));
}
}
输出:{苹果= 1,樱桃= 1,贝里= 1,香蕉= 2}
您的代码正常工作。 else语句只在需要时执行(当给定项目已经在列表中并且只需要增加其数量时)。在这里,我写了一些调试消息给你的代码。运行它,你会看到究竟发生了什么。
package strings;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Testing {
static List<String> list() {
List<String> words = new ArrayList<String>();
words.add("Cherry");
words.add("Banana");
words.add("Apple");
words.add("Banana");
words.add("Berry");
words.add("Banana");
words.add("Berry");
words.add("Banana");
return words;
}
static Map<String, Integer> ArrayFrequencies(List<String> words) {
Map<String, Integer> frequencies = new HashMap<String, Integer>();
for (String element : words) {
if (!frequencies.containsKey(element)) {
System.out.println("Seems like => " + element + " is not in the list yet. Adding it");
frequencies.put(element, 1);
} else {
System.out.println("Seems like => " + element + " is in the list already. Incrementing its count from : " +
frequencies.get(element) + " => to : " + (frequencies.get(element) + 1));
frequencies.put(element, frequencies.get(element) + 1);
}
}
return frequencies;
}
public static void main(String[] args) {
System.out.println(ArrayFrequencies(list()));
}
}
输出:
Seems like => Cherry is not in the list yet. Adding it
Seems like => Banana is not in the list yet. Adding it
Seems like => Apple is not in the list yet. Adding it
Seems like => Banana is in the list already. Incrementing its count from : 1 => to : 2
Seems like => Berry is not in the list yet. Adding it
Seems like => Banana is in the list already. Incrementing its count from : 2 => to : 3
Seems like => Berry is in the list already. Incrementing its count from : 1 => to : 2
Seems like => Banana is in the list already. Incrementing its count from : 3 => to : 4
{Apple=1, Cherry=1, Berry=2, Banana=4}
if(!frequencies.containsKey(elements))
{
//put the data in map for the first time
//Suppose Element "Apple" entering for the first time
frequencies.put(elements, 1);
} else {
//put the data in map if map already contains that element
//Element "Apple entering for the second time". Here it will get previous count and increase by one
frequencies.put(elements, frequencies.get(elements) + 1);
}
非常感谢!很有帮助。我现在更了解它! – IGJ
@IGJ如果你愿意,你可以把它标记为正确的。 WC –
什么我比较困惑的是,为什么 “其他” 块被执行以及它如何工作。
的if
块设置地图项为1明确,因为它是第一个值与该elements
键关联。
else
块正在更新映射条目。它存在,它有一个价值,它需要增加。所以你说:
frequencies.get(elements) + 1 //get the prior value and add one to it
frequencies.put(^^^^^^^) //update the map entry with the new value from above.
注意:不能从其他区块应用逻辑的,如果,因为你不能增加空。
谢谢!这非常有帮助! – IGJ
钥匙是否已在地图上它得到执行。例如,你在输入中有两个'Banana',所以'else'在遇到第二个时被执行。 –
如果'!frequencies.containsKey(elements)'没有得到执行else子句(请注意,由于可能导致混淆,变量被称为'elements'而不是'element'')。你问的是错误的问题。 – Gendarme
调试它并按照步骤操作。你的期望什么时候执行什么可能是错误的。 – blafasel