class Test2_2Array {
public static void main(String[] args) {
int[][] arr ={{1,2,3},{4,5,6,7},{8,9}};
print(arr);
getSum(arr);
}
//二维数组的遍历
public static void print(int[][] arr) {
for (int i = 0;i < arr.length;i++ ) {
for (int j = 0;j < arr[i].length ;j++) {
System.out.print(arr[i][j] + ";");
}
System.out.println();
}
}
//二维数组求和
public static void getSum(int[][] arr) {
int sum = 0;
for (int i = 0;i < arr.length ;i++ ) {
int sum1 = 0;
for (int j = 0;j <arr[i].length ;j++ ) {
sum1 += arr[i][j];
}
System.out.println("第"+(i+1)+"组的和是" + sum1);
sum += sum1;
}
System.out.println("sum =" + sum);
}
}
|
|