【问题标题】:Go with for the int without creating an Array?在不创建数组的情况下使用 int?
【发布时间】:2014-10-21 17:20:07
【问题描述】:

我有以下脚本:

    public static void main(String[] args) 
    {
            String[] s1 = {"red", "white", "black", "blue"};
            String[] s2 = {"red", "black", "green"};
            String[] s3 = {"red", "green"};
            int red = 0;
            int black =0;
            int green = 0;
            int white= 0;
            int blue= 0;

            List <String> list = new ArrayList <String>();
            list.addAll(Arrays.asList(s1));
            list.addAll(Arrays.asList(s2));
            list.addAll(Arrays.asList(s3));

           for(String s: list)
           {
               if(s.equals("red"))
                {
                   red++;
                }
               else if(s.equals("white"))
                {
                   white++;
                }
               else if(s.equals("black"))
                {
                   black++;
                }
               else if(s.equals("green"))
                {
                   green++;
                }
               else  if(s.equals("blue"))
                {
                   blue++;
                }

           }
            System.out.println(red + "  Red found\n" + green + "  Green Found\n" + white + "  White Found\n" + black + "  Black Found\n" + blue + "  Blue Found");
    }
}

输出:

3  Red found
2  Green Found
1  White Found
2  Black Found
1  Blue Found

我遇到的问题是我添加为 int,需要继续添加到 System.out.println,有一些方法可以在不创建数组的情况下使用 int?

【问题讨论】:

标签: java arrays for-loop


【解决方案1】:

也许声明一张预期颜色的地图并将其用于计数:

public static void main(String[] args) 
{
    HashMap<String, Integer> allColorCount;

    //optionally pre-populate your map if you want it to be static colors
    allColorCount.put("blue", 0);
    //... repeat for all colors
    allColorCount.put("white", 0);

    /* 
     * your orig code for populating your lists
     */
     for(String s: list)
     {
        if(allColorCount.containsKey(s))
        {
            allColorCount.put(s, allColorCount.get(s)++);
        }
        //or if you want to dynamically map your counts:
        else
        {
           allColorCount.put(s, 1);
        }
     }

    //then at the end: 
    Set<String> colorSet = allColorCount.keyset();
    for(String currColor : colorSet)
    {
         System.out.println(allColorCount.get(currColor) + " " + currColor + " Found");
    }
}

【讨论】:

  • 您必须将 allColorCount 声明为 main 外部的静态,否则静态 main() 无法看到它,因为它是一个非静态属性 - 编辑:您更改了代码
  • 问题是命令中:allColorCount.get(s)++“s”是String,所以不让我加值。
  • 你有问题。 allColorCount 是一个哈希图,其值为 Integer
  • 那是为了填充您的列表而必须在您的原始代码中添加的内容?我想我在那里做错了什么。
  • 看看你以前的问题和答案。您似乎只是在不了解发生了什么的情况下继续重新发布建议。
【解决方案2】:

如果我理解你需要的是一个 HashMap。您将检查地图是否包含例如键“红色”。如果它不包含它,则插入 否则将找到的值增加一

HashMap<String,Integer> map = new HashMap<>();
String input = "red";
//for each input you will be doing:
if (!map.containsKey(input)) {

    map.put(input,0)
}
else {
    map.put(input,map.get(input)+1);
}

另外,如果你有一个 Collection/ArrayList 我相信你也可以这样做:

int redOccurrences = Collections.frequency(colorsList, "red");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多