字符串数组中的字符频率

时间:2017-03-06 04:20:30

标签: java

这应该是对抗某个角色的数量,但它会一直返回零。这是我在主System.out.println中调用的(“频率'e'=”+ charFrequency(stringArray,'e'));

public static int charFrequency(String[] s, char ch){
    int counter=0;

    for(int i=0; i<s.length; i++){
        if(s.equals(ch)){
            counter++;
        }
        else{
            return 0;
        }

    }
    return counter;

6 个答案:

答案 0 :(得分:3)

您正在检查String数组的字符。您需要针对数组中的char检查char

if(s[i].charAt(j) ==ch){
     counter++;
 }

你回来错了。

所以你的代码变成了

   public static int charFrequency(String[] s, char ch) {
        int counter = 0;

        for (int i = 0; i < s.length; i++) {
            for (int j = 0; j < s[i].length(); j++) {
                if (s[i].charAt(j) == ch) {
                    counter++;
                }
            }
        }

        return counter;
    }

答案 1 :(得分:3)

如果您要对不同的字符多次使用此方法,那么收集所有频率并使用此数据是合理的:

public static Map<Character, Long> charFrequency(String[] s){
    return Stream.of(s)
            .map(CharSequence::chars)
            .flatMap(x -> x.mapToObj(y -> (char)y))
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}

public void test() {

    String s[] = {"banana", "apple", "orange"};
    Map<Character, Long> frequencies = charFrequency(s);

    System.out.println(frequencies);
    System.out.println(frequencies.get('a'));
}

输出:

{p=2, a=5, r=1, b=1, e=2, g=1, l=1, n=3, o=1}
5

答案 2 :(得分:1)

为什么不要&#39;你使用foreach而不是每次循环?

private static int findFrequency(String[] array, character character){
    int totalCount = 0;
    for(int i=0;i<array.length;i++) {
        for(character ch : array[i].toCharArray()){
            if(ch == c)
                totalCount++;
        }
    }
    return totalCount;
}

答案 3 :(得分:1)

你可以非常轻松地做到这一点:没有任何for循环的最快方式

只需一行代码:

return str.split(Pattern.quote(""+ch), -1).length-1;

答案 4 :(得分:0)

以下是如何实现这一目标:

public static int charFrequency(String[] stringArray, char ch)
{
    int counter = 0;

    // loop through each String in the array
    for (int i = 0; i < stringArray.length; i++)
    {
        String s = stringArray[i];

        // in case the string is null
        if (s != null)
        {
            // loop through each char in the string
            for (int j = 0; j < s.length(); j++)
            {
                if (s.charAt(j) == ch)
                    counter++;
            }
        }
    }
    return counter;
}

答案 5 :(得分:0)

Dmitry Gorkovets's answer的传统for loop版本:

public static void main(String[] args) throws Exception {
    int freq[] = getFrequency(new String[] {"banana", "apple", "orange"});
    System.out.println("Frequency of a : "+freq['a']);
    System.out.println("Frequency of p : "+freq['p']);
}

static int[] getFrequency(String inputs[]) {
    int op[] = new int[150]; // The max ascii is of 'z' which is 122.
    for (String ip : inputs) {
        char c[] = ip.toCharArray();
        for (char x : c) {
            op[x]++;
        }
    }
    return op;
}

输出:

Frequency of a : 5
Frequency of p : 2