我有一个Java的二维数组,需要查找其中的最大值,并打印出它在数组中出现的次数。
我尝试了以下方法,但没有成功。
int[][] rand = new int[][]{
{1, 80, 3, 4, 5},
{13, 199, 80, 8},
{12, 22, 80, 190}
};
int max = rand[0][0];
int count = 0;
for (int i = 0; i < rand.length; i++){
for (int ii = 0; ii < rand[i].length; ii++) {
if (rand[i][ii] > max) {
max = rand[i][ii];
count++;
}
}
}
System.out.println(max + " " + count);
rand[0][0]
将引发OutOfBoundsException异常。 - pxcv7r