2017-09-24 139 views
0

我想我明白了第一個說法。這就是說,如果「單詞」中的項目不在「頻率」中,則添加它們併爲其賦值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}

+0

鑰匙是否已在地圖上它得到執行。例如,你在輸入中有兩個'Banana',所以'else'在遇到第二個時被執行。 –

+1

如果'!frequencies.containsKey(elements)'沒有得到執行else子句(請注意,由於可能導致混淆,變量被稱爲'elements'而不是'element'')。你問的是錯誤的問題。 – Gendarme

+0

調試它並按照步驟操作。你的期望什麼時候執行什麼可能是錯誤的。 – blafasel

回答

0

您的代碼正常工作。 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} 
+0

非常感謝!這非常有幫助!感謝您花時間創建調試消息。我現在知道了。謝謝!! – IGJ

+0

不用客氣@IGJ。很高興這有助於! – zee

0
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); 
    } 
+0

非常感謝!很有幫助。我現在更瞭解它! – IGJ

+0

@IGJ如果你願意,你可以把它標記爲正確的。 WC –

0

什麼我比較困惑的是,爲什麼 「其他」 塊被執行以及它如何工作。

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. 

注意:不能從其他區塊應用邏輯的,如果,因爲你不能增加空。

+0

謝謝!這非常有幫助! – IGJ