日期:2014-05-20 浏览次数:20970 次
public class FindMax {
public static void main(String[] args) {
int[][] arr = { { 70, 12, 20, 13, 16 }, { 30, 21, 13, 17, 18 },
{ 18, 12, 50, 75, 31 }, { 32, 33, 25, 24, 40 }, };
int max = 0;
int row = -1;
int col = -1;
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
if(arr[i][j]>max){
max = arr[i][j];
row = i;
col = j;
}
}
}
System.out.println("最大元素是:"+max+" 位置是 i="+row+" j="+col);
}
}
------解决方案--------------------
这个很简单的啊,下面是最直接的比较方法,你看看,其实最重要的是自己多去思考思考。
public static void GetMaxNbr1(int[][] a) {
int max, x, y;
if (a.length > 0 && a[0].length > 0) {
x=0;
y=0;
max = a[x][y];
for (int i = 0; i < a.length; i++) {
for (int j = 1; j < a[0].length; j++) {
if(a[i][j]>max)
{
x=i;
y=j;
max = a[i][j];
}
}
}
System.out.println("数组a数据最大值位置是:a["+x+"]["+y+"];");
System.out.println("数组a数据最大值是:"+a[x][y]);
}
}