为什么"其他"为此for循环执行的代码块?

时间:2017-09-24 20:47:54

标签: java

我想我理解第一个陈述。它说,如果"单词"中的项目不在"频率",然后添加它们并为每个赋值1,对吧? 我更困惑的是为什么" 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}

3 个答案:

答案 0 :(得分: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 );
    }

答案 1 :(得分:0)

  

我更加困惑的是为什么“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。

答案 2 :(得分: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}