Java 二维数组整数序列

Java 二维数组整数序列,java,arrays,arraylist,2d,Java,Arrays,Arraylist,2d,我正在尝试计算一个整数在二维数组中出现的次数,并打印计数。我是Java新手,还在学习 这是我正在使用的2D阵列: System.out.println("Array\t Nummbers"); int Array[][] = { { 39, 16, 13, 1, 35, 31, 47 }, { 39, 2, 45, 44, 38, 19, 40 }, { 39, 43, 33, 16, 20, 9, 30 }, { 42, 23, 26, 27, 1, 48, 4

我正在尝试计算一个整数在二维数组中出现的次数,并打印计数。我是Java新手,还在学习

这是我正在使用的2D阵列:

System.out.println("Array\t Nummbers");
    int Array[][] = { { 39, 16, 13, 1, 35, 31, 47 }, { 39, 2, 45, 44, 38, 19, 40 }, { 39, 43, 33, 16, 20, 9, 30 },
            { 42, 23, 26, 27, 1, 48, 47 }, { 18, 25, 47, 31, 46, 36, 20 }, { 21, 34, 19, 6, 4, 25, 29 },
            { 30, 5, 22, 23, 11, 47, 19 }, { 34, 30, 7, 31, 16, 45, 41 }, { 37, 9, 7, 42, 45, 24, 5 },
            { 39, 34, 28, 5, 37, 17, 22 }, { 42, 47, 34, 15, 35, 41, 28 }, { 22, 8, 46, 30, 36, 28, 11 };
我正在使用我在这里找到的以下代码,我做了一些更改,但仍然无法修复它。

System.out.println("\t" + countCharInRow(Array));
public static Map<Character, Integer> countCharInRow(char[][] myArray) {
 Map<Character, Integer> charOccurences = new HashMap<>();
 Set<Character> rowCharacters = new HashSet<>();
 for(char[] row : myArray) {
    for(char charInArray = row) { // put all the chars on the line in the set
        rowCharacters.add(row);
    }
    for(char charInSet : rowCharacters) {
        Integer occurences = charOccurences.get(charInSet);
        if(occurences == null) { // first occurence
            charOccurences.put(charInSet, 1);
        } else {  // increment
            charOccurences.put(charInSet, occurences.intValue() + 1);
        }
    }
 }
 return charOccurences;
}
System.out.println(“\t”+countCharInRow(数组));
公共静态映射countCharInRow(char[][]myArray){
Map charoccurrences=new HashMap();
Set rowCharacters=new HashSet();
对于(字符[]行:myArray){
对于(char charInArray=row){//将所有字符放在集合中的行上
行字符。添加(行);
}
for(字符集:行字符){
整数发生率=charoccurrences.get(charInSet);
如果(occurrences==null){//第一次出现
查林塞特(charInSet,1);
}else{//increment
charoccurrences.put(charInSet,occurrences.intValue()+1);
}
}
}
返回事件;
}
有什么建议吗?

公共静态映射countCharInMatrix(char[]]mat){
public static Map<Character, Integer> countCharInMatrix(char[][] mat) {
    Map<Character, Integer> charOccurences = new HashMap<>();
    for(char[] row : mat) {
        for(char charInRow : row) {
            Integer occurences = charOccurences.get(charInRow);
            if(occurences == null) { // first occurence
                charOccurences.put(charInRow, 1);
            } else {  // increment
                charOccurences.put(charInRow, occurences.intValue() + 1);
            }
        }
    }
    return charOccurences;
}
Map charoccurrences=new HashMap(); 对于(字符[]行:mat){ for(char charInRow:row){ 整数发生率=charoccurrences.get(charInRow); 如果(occurrences==null){//第一次出现 charInRow,1; }else{//increment charoccurrences.put(charInRow,occurrences.intValue()+1); } } } 返回事件; }
问题不是用英语写的。请解释清楚。你的意思是想让函数生成2d数组中所有整数频率的映射吗?谢谢。。。试图找出并打印出每个数字在这个大数组中出现了多少次……希望你能理解,抱歉我的英语不好!因此,如果
'a'
在第一行中出现3次,在第二行中出现2次,您希望将
'a':5
添加到地图中吗?是的,先生,事实上是这样..因此无需首先将字符放入集合中。事实上,这将使您最多对一行中的每个字符计数一次,因为集合中没有重复的字符。以这种精神添加了一个答案。感谢您的回答。。。与system.out.print中的最后一个错误相同,methodFinal类型中的方法countCharInMatrix(char[][])不适用于参数(int[][]),当更改为int时,更大的问题开始……如果发送int矩阵,则需要将矩阵参数的类型更改为
int[][
,局部变量和返回类型中的
Character
Integer
,以及循环中的
char
int
感谢您修复我的矩阵m8。。。。我真的很感激!!!如果我的想法行得通,我也会把它寄给你,再要些现金;)你应该知道我的矩阵是什么创造的,4=4,4=4,5=5,5=5,6=4,6=4,7=4,7=4,8=6,7=4,7=4,7=4,7=4,7=4,7=4,6=4,6=4,你应该知道你应该知道我的矩阵是什么我的矩阵是什么我的矩阵是什么我的矩阵是什么我的矩阵是什么我的创造的矩阵是什么我的矩阵是我的矩阵是什么我的创造的,4,4,7=4,7=4,7,7=4,7=4,7=4,7=4,7,7=4,7,7,7=4,7,7,7=4,7,7,7,8=4,7,7,8=4,7,7,8=6,8=6,7,7,8=6,7,9=4,7,7,7,9=4,7,7,,46=4,47=8,48=2,49=3}