gpt4 book ai didi

Java - 列多数组中的最大数字

转载 作者:行者123 更新时间:2023-11-30 03:34:39 24 4
gpt4 key购买 nike

看来我在以下代码中超出了范围(请参阅带有注释的行),并且我似乎无法解决问题。我在这里忽略或忘记了什么吗?

int[][] num = {   {10,2,5},
{5,1,0},
{25,35,16,20,19},
{26,27,100} };

for (int col = 0; col < num.length; col++) {
int highest = Integer.MIN_VALUE;
for (int row = 0; row < num[col].length + 1; row++)
if (num[row][col] > highest) //this is where I get the error.
highest = num[row][col];
System.out.println( "Highest number in column " + col + " = " + highest);
}

最佳答案

// Ok, first find the longest row:
int longest = Integer.MIN_VALUE;
for (int[] row : num) {
longest = Math.max(longest, row.length);
}

// then, create an array to store max column values:
int[] rowHigh = new int[longest];
for (int i = 0; i < longest; i++) {
rowHigh[i] = Integer.MIN_VALUE;
}

// then iterate over initial array to find max values of each column
for (int[] row : num) {
for (int i = 0; i < row.length; i++) {
rowHigh[i] = Math.max(rowHigh[i], row[i]);
}
}

关于Java - 列多数组中的最大数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28277049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com