猿问

如何在Java中使用count sub方法和目标字符串计算添加到列表数组中的字符串?

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 中的次数,然后将其返回到 main 以便可以打印。我能做些什么来解决这个问题?


湖上湖
浏览 162回答 3
3回答

MYYA

这是因为您正在打印列表而不是您的countOccurence方法的结果。要解决这个问题 -将结果存储countOccurence在变量中int&nbsp;count&nbsp;=&nbsp;countOccurence("Go",&nbsp;y);在下一行打印计数。System.out.println(count);

Helenr

只是一个可选的解决方案。在 中Java8,您可以Stream API通过计数出现次数来做到这一点。Map<String, Long> counts =&nbsp; &nbsp; &nbsp; &nbsp; listOfString.stream().collect(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.groupingBy(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Function.identity(), Collectors.counting()));和结果代码;public class Remove {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList< String > listOfString = new ArrayList < > ();&nbsp; &nbsp; &nbsp; &nbsp; listOfString.add("Go");&nbsp; &nbsp; &nbsp; &nbsp; listOfString.add("Home");&nbsp; &nbsp; &nbsp; &nbsp; listOfString.add("Go");&nbsp; &nbsp; &nbsp; &nbsp; listOfString.add("Home");&nbsp; &nbsp; &nbsp; &nbsp; Map<String, Long> counts =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfString.stream().collect(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.groupingBy(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Function.identity(), Collectors.counting()));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(counts.get("Go"));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(counts.get("Home"));&nbsp; &nbsp; }}

慕虎7371278

打印事件:System.out.println("Occurence of 'Go' is " + countOccurence("Go", y));&nbsp;你也应该删除的变化i <= t.size();来i < t.size();,以避免IndexOutOfBounds错误。public static void main(String[] args) {&nbsp; &nbsp; ArrayList<String> y = new ArrayList<>();&nbsp; &nbsp; y.add("Go");&nbsp; &nbsp; y.add("Home");&nbsp; &nbsp; y.add("Go");&nbsp; &nbsp; y.add("Home");&nbsp; &nbsp; System.out.println(y);&nbsp; &nbsp; System.out.println("Occurence of 'Go' is " + countOccurence("Go", y));}public static int countOccurence(String y, ArrayList<String> t) {&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; for (int i = 0; i < t.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (y.equalsIgnoreCase(t.get(i))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return count;}
随时随地看视频慕课网APP

相关分类

Java
我要回答