package arithmetic;
/**
* 对于二维数组,每一行的最大值集合里一定有整个数组的最大值
* 对于每一列的最大值,逻辑是类似的
* @author zengsht
*
*/
public class MaxArray {
public static void main(String[] args) {
int[][] num = {
{ 2, 4, 3, 1, 0 },
{ 5, 6, 8, 0, 7 },
{ 9, 0, 1, 2, 0 },
{ 3, 4, 5, 6, 0 },
{ 21, 23, 0, 45, 2}
};
//存储每一行的最大值
int[] temp1 = new int[5];
for (int i = 0; i < 5; i++) {
temp1[i] = num[i][0];//假定每行的第一个是最大的
for (int j = 0; j < 4; j++) {
if(num[i][j+1]>temp1[i]){
//比当前的最大值大,最大值被替换
temp1[i] = num[i][j+1];
}
}
System.out.println("第"+i+"行的最大值是:"+temp1[i]);
}
}
}