public static void main(String[] args) {
int [][]arr={
{11,22,33},
{44,55,66}
};
printArray(arr);
int sum= getSum(arr);
System.out.println("求和累加为:"+sum);
}
/*
二维数组的遍历操作:int [][]arr={{11,22,33},{44,55,66}}
1.遍历二维数组,获取到每一个一维数组
2.继续遍历一维数组,获取到每一个元素
*/
public static void printArray(int [][]arr){
for (int i = 0; i < arr.length; i++) {
int [] temp=arr[i];
for (int j = 0; j < temp.length; j++) {
System.out.println(arr[i][j]);
}
}
}
/*
遍历二维数组并求和;int [][]arr={{11,22,33},{44,55,66}}
对内部存储的元素进行累加求和,并将结果输出在控制台
*/
public static int getSum(int [][] arr){
int sum=0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
sum +=arr[i][j];
}
}
return sum;
}
}
标签:arr,遍历,int,sum,++,二维,数组
From: https://blog.51cto.com/u_15988249/6212523