显示二维数组中的出现次数

时间:2014-01-18 14:10:54

标签: java arrays

我想在二维数组中显示一个角色的出现,到目前为止我已经达到了这些:

public static void main (String [] args)

    {
        char [][] alphabet = {{'B', 'A', 'A', 'W'}, {'L', 'U', 'J', 'A'}, {'B', 'A', 'Z', 'A'}};

        displayArray(alphabet);
    }

    public static void displayArray (char [][] array)
    {
        for (int r = 0; r < array.length; r++)
        {
            for (int c = 0; c < array[r].length; c++)
            {
                System.out.print(array[r][c] + " ");
            }

            System.out.println(" ");
        }

在这种情况下,通常出现的字符是'A',我如何显示'A'是常见的重复?

3 个答案:

答案 0 :(得分:2)

您只需使用SetMap

即可

HashSet是添加字符的好选项,并使用contains方法检查它们是否存在。

HashSet<Integer> uniqueLetters = new HashSet<Integer>();

        for (int r = 0; r < array.length; r++)
        {
            for (int c = 0; c < array[r].length; c++)
            {

                if(uniqueLetters.contains((int)(array[r][c])))
                System.out.println(array[r][c] +" already exists.");
                else
                {
                uniqueLetters.add((int)array[r][c] );
                System.out.print(array[r][c] + " added.");
                }
            }

            System.out.println(" ");
        }

另一种选择是使用HashMap并将计数器作为值。每次查看角色时都会增加计数器。

答案 1 :(得分:0)

您可以使用HashMap对每个字符进行排名,并在遇到该字符时递增。检查每个字符是否已经存在,如果没有初始化为1:

public static void displayArray(char[][] array) {
        Map<Character, Integer> rankMap = new HashMap<Character, Integer>();
        for (int r = 0; r < array.length; r++) {
            for (int c = 0; c < array[r].length; c++) {
                if (rankMap.get(array[r][c]) != null) {
                    rankMap.put(array[r][c], rankMap.get(array[r][c])+1);
                } else {
                    rankMap.put(array[r][c], 1);
                }
                System.out.print(array[r][c] + " ");
            }

            System.out.println(" ");
        }
        for (char c : rankMap.keySet().toArray(
                new Character[rankMap.keySet().size()])) {
            System.out.println("Character: " + c + " " + rankMap.get(c) + " times");

        }
    }

遍历HashMap以获取每个字符的计数值。

<强>输出:

B A A W  
L U J A  
B A Z A  
Character: U 1 times
Character: W 1 times
Character: A 5 times
Character: B 2 times
Character: L 1 times
Character: J 1 times
Character: Z 1 times

答案 2 :(得分:0)

import java.util.LinkedHashMap;
import static java.lang.System.out;

class Demo {

public static void main(String[] args) {
    char[][] alpha = {{'B', 'A', 'A', 'W'}, {'L', 'U', 'J', 'A'}, {'B', 'A', 'Z', 'A'}};
    info(alpha);
}

private static void info(char[][] arr) {
    LinkedHashMap<Character, Integer> stats = new LinkedHashMap<Character, Integer>();
    boolean flag;
    int cnt;
    out.println("=======================");
    for (char[] ar : arr) {
        for (char c : ar) {
            flag = stats.containsKey(c);
            cnt = flag ? stats.get(c) : 0;
            cnt ++;
            stats.put(c, cnt);
            out.printf("%c ", c);
        }
        out.println();
    }
    out.println("=======================");
    for (char c : stats.keySet()) {
        out.printf("%c, occured %d time(s)%n", c, stats.get(c));
    }
}
}

=======================
B A A W 
L U J A 
B A Z A 
=======================
B, occured 2 time(s)
A, occured 5 time(s)
W, occured 1 time(s)
L, occured 1 time(s)
U, occured 1 time(s)
J, occured 1 time(s)
Z, occured 1 time(s)