2017-09-24 6 views
0

私は最初の声明を理解すると思います。言葉の中の項目が「頻度」にない場合は、それらを追加し、それぞれに1の値を割り当てます。 私がもっと混乱しているのは、なぜ "else"ブロックが実行され、どのように動作するのかです。私はアウトプットを理解していますが、それがどのように機能するかは分かりません特定の単語が複数回現れることは明らかですが、それをどのように認識していますか?もう一度、なぜ最初の文が真であれば "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'を2回入れたので、2回目に遭遇すると' else'が実行されます。 –

+1

'!frequencies.containsKey(elements)'(変数が混乱の原因となる可能性があるため、 'element'の代わりに' elements'ではなく 'elements'と呼ばれていることを知らないことに注意してください)のelse節は実行されません。あなたは間違った質問をしています。 – 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ブロックは、elementsキーに関連付けられている最初の値であるため、マップエントリを明示的に1に設定します。

ブロック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. 

は注意:あなたがnullをインクリメントすることはできませんので、あなたが、場合に他のブロックからのロジックを適用することはできません。

+0

ありがとうございました!これはとても役に立ちました! – IGJ

関連する問題