[英]Why is the “else” code block executed for this for loop?
我想我明白第一句話。 這就是說,如果“單詞”中的項目不在“頻率”中,則將它們相加並為每個項目賦值1,對不對? 我更困惑的是為什么執行“ else”塊及其工作方式。 我了解輸出,但不太清楚它是如何工作的。 它顯然認識到一個特定的單詞出現不止一次,但是它如何識別呢? 再一次,如果第一個語句為真,為什么它將轉到“ 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()));
}
}
輸出:{Apple = 1,Cherry = 1,Berry = 1,Banana = 2}
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 );
}
我更困惑的是為什么執行“ else”塊及其工作方式。
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.
注意 :您不能將else塊中的邏輯應用於if,因為您不能遞增null。
您的代碼表現正常。 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}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.