计算字母表中每个字母出现在ArrayList中的一系列字符串中的次数

时间:2016-03-12 01:10:05

标签: java arrays string arraylist char

我有一个ArrayList,以"遛狗"的形式存储字符串或注释。我有一个notes类,其中的方法可以打印每个字母出现在整个ArrayList中的次数。我应该声明并使用大小为26的整数数组,并使用String类中的charAt方法将笔记本中的每个字母转换为char。然后我必须使用该char来索引到低级数组中的适当位置。到目前为止,这是我的方法,但还没有完成:

public void printLetterDistribution() {
        ArrayList<Integer> aList = new ArrayList<Integer>();
        for (int i = 0; i < notes.size(); i++) {
            String note = notes.get(i);
            for (int j = 0; j < note.length(); j++) {
                char letter = note.charAt(j);
                int code = (int)letter;
                aList.add(code);
            }
        }
        Collections.sort(aList);

    }

我撞墙了,我不知道如何继续。正如您所看到的,我已尝试将字母转换为字符代码,但这可能不是最好的方法,而且我仍然卡住了。有人可以帮忙吗?

编辑 - 以下是整个笔记类:

public class Notebook {
    private ArrayList<String> notes;

    public Notebook() { notes = new ArrayList<String>(); }

    public void addNoteToEnd(String inputnote) {
        notes.add(inputnote);
    }

    public void addNoteToFront(String inputnote) {
        notes.add(0, inputnote);
    }

    public void printAllNotes() {
        for (int i = 0; i < notes.size(); i++) {
            System.out.print("#" + (i + 1) + " ");
            System.out.println(notes.get(i));
        }
        System.out.println();
    }

    public void replaceNote(int inputindex, String inputstring) {
        int index = inputindex - 1;
        if (index > notes.size() || index < 0) {
            System.out.println("ERROR: Note number not found!");
        } else {
            notes.set(index, inputstring);
        }
    }

    public int countNotesLongerThan(int length) {
        int count = 0;
        for (int i = 0; i < notes.size(); i++) {
            String temp = notes.get(i);
            if (temp.length() > length) {
                count++;
            }
        }
        return count;
    }

    public double averageNoteLength() {
        int sum = 0;
        for (int i = 0; i < notes.size(); i++) {
            String temp = notes.get(i);
            int length = temp.length();
            sum += length;
        }
        double average = (double)(sum / notes.size());
        return average;
    }

    public String firstAlphabetically() {
        String min = "";
        for (int i = 0; i < notes.size(); i++) {
            for (int j = i + 1; j < notes.size(); j++) {
                if ((notes.get(i)).compareTo(notes.get(j)) < 0) {
                    min = notes.get(i);
                } else {
                    min = notes.get(j);
                }
            }
        }
        return min;
    }

    public void removeNotesBetween(int startnote, int endnote) {
        int start = startnote - 1;
        int end = endnote - 1;
        for (int i = end - 1; i > start; i--) {
            notes.remove(i);
        }
    }

    public void printNotesContaining(String findString) {
        for (int i = 0; i < notes.size(); i++) {
            if (notes.get(i).contains(findString)) {
                System.out.println("#" + i + " " + notes.get(i));
            }
        }
    }

    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; j < note.length(); j++) {
                if (note.charAt(j) == letter.charAt(0)) {
                    count++;
                }
            }

        }
        return count;
    }

    public void findAndReplaceFirst(String old, String newWord) {
        for (int i = 0; i < notes.size(); i++) {
            String note = notes.get(i);
            if (note.contains(old)) {
                int loc = note.indexOf(old);
                int len = old.length();
                String temp = note.substring(0, loc ) + note.substring(loc + len, note.length());
                String newString = temp.substring(0, loc) + newWord + temp.substring(loc, temp.length());
                notes.set(i, newString);
            } else {
                String newString = note;
                notes.set(i, newString);
            }
        }
    }

    public void printLetterDistribution() {
        int[] p = new int[26];
        for (int i = 0; i < 26; i++) {
            p[i] = 0;
        }
        for (int i = 0; i < notes.size(); i++) {
            String note = notes.get(i);
            note = note.toLowerCase();
            for (int j = 0; j < note.length(); j++) {
                char letter = note.charAt(j);
                p[letter - 'a']++;
            }
        }
        System.out.println(p);
    }

}

1 个答案:

答案 0 :(得分:1)

您可以使用长度为26的int数组并增加索引字母的数量 - &#39; a&#39;;

int[] p = new int[26];
for(int i = 0; i < 26; i++) p[i] = 0;
for (int i = 0; i < notes.size(); i++) {
        String note = notes.get(i);
        for (int j = 0; j < note.length(); j++) {
            char letter = note.charAt(j);
             if(letter>= 'a' && letter <= 'z')
               p[letter-'a']++;

 }

PS:我假设笔记只是小写的。如果不是这种情况,请使用note.toLowerCase()将它们降低。

因为在你的笔记中你可以有空格,我已经更新了代码。