计算ArrayList中给定字符串的出现次数

lol*_*ola 5 java arraylist

我有一个字符串列表,我浏览它并计算"x"字符串的数量如下,但计数不打印我预期的值:

ArrayList<Integer> list = new ArrayList<Integer>();

List<String> strings = table.getValue(); //this gives  ["y","z","d","x","x","d"]

int count = 0;
for (int i = 0; i < strings.size(); i++) {
    if ((strings.get(i) == "x")) {
        count++;
        list.add(count);
    }
}

System.out.println(list);
Run Code Online (Sandbox Code Playgroud)

这给[]它应该是2因为我有2次出现"x"

归档时间:

查看次数:

16561 次

最近记录:

14 年,7 月 前