如何使用Java中的count子方法和目标字符串对添加到列表数组的字符串进行计数?

时间:2018-09-20 14:20:57

标签: java string arraylist methods

public class Remove {
    public static void main(String[] args) {
        ArrayList < String > y = new ArrayList < > ();
        y.add("Go");
        y.add("Home");
        y.add("Go");
        y.add("Home");
        System.out.println(y);
        countOccurence("Go", y);
        System.out.println(y);
    }
    public static int countOccurence(String y, ArrayList < String > t) {
        int count = 0;
        for (int i = 0; i <= t.size(); i++) {
            if (y.equalsIgnoreCase(t.get(i))) {
                count++;
            }
        }
        return count;
    }
}

此代码不允许我打印子方法countOccurence返回的计数。此代码应计算目标字符串出现在数组Y中的时间,然后将其返回到主数组,以便可以打印。我该怎么做才能解决此问题?

4 个答案:

答案 0 :(得分:3)

这是因为您正在打印列表,而不是您的countOccurence方法的结果。要解决-

countOccurence的结果存储在变量中

int count = countOccurence("Go", y);

在下一行中打印计数。

System.out.println(count);

答案 1 :(得分:1)

只是可选解决方案。Java8中,您可以使用Stream API进行计数。

Map<String, Long> counts =
        listOfString.stream().collect(
                Collectors.groupingBy(
                        Function.identity(), Collectors.counting()));

和结果代码;

public class Remove {
    public static void main(String[] args) {
        ArrayList< String > listOfString = new ArrayList < > ();
        listOfString.add("Go");
        listOfString.add("Home");
        listOfString.add("Go");
        listOfString.add("Home");

        Map<String, Long> counts =
                listOfString.stream().collect(
                        Collectors.groupingBy(
                                Function.identity(), Collectors.counting()));

        System.out.println(counts.get("Go"));
        System.out.println(counts.get("Home"));
    }
}

答案 2 :(得分:1)

要打印事件:

System.out.println("Occurence of 'Go' is " + countOccurence("Go", y)); 

您还应该删除对i <= t.size();的更改i < t.size();,以避免发生IndexOutOfBounds错误。

public static void main(String[] args) {
    ArrayList<String> y = new ArrayList<>();
    y.add("Go");
    y.add("Home");
    y.add("Go");
    y.add("Home");
    System.out.println(y);
    System.out.println("Occurence of 'Go' is " + countOccurence("Go", y));
}

public static int countOccurence(String y, ArrayList<String> t) {
    int count = 0;
    for (int i = 0; i < t.size(); i++) {
        if (y.equalsIgnoreCase(t.get(i))) {
            count++;
        }
    }
    return count;
}

答案 3 :(得分:0)

您可以直接致电

System.out.println(countOccurrence("Go", y));

正确的代码是:

import java.util.ArrayList;

public class Remove {
    public static void main(String[] args) {
        ArrayList < String > y = new ArrayList < > ();
        y.add("Go");
        y.add("Home");
        y.add("Go");
        y.add("Home");
        System.out.println(y);
        System.out.println(countOccurence("Go", y));
    }
    public static int countOccurence(String y, ArrayList < String > t) {
        int count = 0;
        for (int i = 0; i < t.size(); i++) {
            if (y.equalsIgnoreCase(t.get(i))) {
                count++;
            }
        }
        return count;
    }
}

或者,如果您想重用该值,只需将其保存在一个变量中,如sneh建议的那样