计数特定字母的数量串联字符串出现在一个ArrayList [英] Counting the number of a certain letter appears in series of strings in an ArrayList

查看:121
本文介绍了计数特定字母的数量串联字符串出现在一个ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在做的菜的形式储存注意事项(字符串)一个ArrayList。我有一个应该通过ArrayList中所有的笔记和计数的时间出现一定的字母数字的方法的笔记类。这是到目前为止,我写的方法:

I have an ArrayList that stores notes (strings) in the form of "do the dishes". I have a notes class with a method that is supposed to go through all the notes in the ArrayList and count the number of times a certain letter appears. This is the method I have written so far:

public int countNumberOf(String letter) {
        int count = 0;
        for (int i = 0; i < notes.size(); i++) {
            String note = (notes.get(i));
            for (int j = 0; i < note.length(); i++) {
                String letterTemp = note.substring(i, i);
                if (letterTemp.equalsIgnoreCase(letter)) {
                    count++;
                }
            }

        }
        return count;
    }

然而,当我运行该程序,我得到零次字母数字一出现,即使我知道它在ArrayList的present。我在做什么错了?

However, when I run the program, I'm getting zero for the number of times the letter "a" appears even though I know it's present in the ArrayList. What am I doing wrong?

推荐答案

/ code>,你可以随时访问和比较的字符,例如:

Instead of passing a String to a method and doing a substring, you can always access and compare the characters, e.g.:

public int countNumberOf(char letter) {
...
String letterTemp = note.charAt(i);
if (letter == letterTemp) {
        count++;
}

另外一个例子:

public static int countOccurrences(String text, char character) {
    int count = 0;
    for (int i = 0; i < text.length(); i++) {
        if (text.charAt(i) == character) {
            count++;
        }
    }
    return count;
}

这篇关于计数特定字母的数量串联字符串出现在一个ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆