1. 程式人生 > >java如何求二維陣列的和、最大值和最小值。

java如何求二維陣列的和、最大值和最小值。

public class SecondArray {
    public static void main(String[] args) {

//int a[][]= new int[][]{{121,3,121},{121,1212,1212},{121,1212,1212}};//靜態分配陣列空間
int a[][] = new int[3][];
a[0] = new int[]{91, 100, 62};
a[1] = new int[]{55, 111, 68};
a[2] = new int[]{1, 49, 90};//動態分配陣列空間
int sum1 = 0;
        for (int 
i = 0; i < a.length; i++) { int sum = 0; for (int j = 0; j < a[i].length; j++) { sum += a[i][j];//每一排相加 } System.out.println(sum); sum1 += sum;//將每一排的加起來 } System.out.println(sum1); int max = 0; int min = a[0][0]; for (int
i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length - 1; j++) { if (max < a[i][j]) { max = a[i][j];//算出最大值 } if (min > a[i][j]) { min = a[i][j];//算出最小值 } } } System.out.println(max);
System.out.println(min); } }