java 数组(二)

public class ArrayDemo{
	public static void main(String[] args){
		int[] arr = {1,5,3,8,2,9,17,13};
		getMaxVal(arr);
		
		int[][] arr1 = {{1,2,3},{4,5},{8,9,10}};
		getSumVal(arr1);
	}
	
	//一维数组求最值
	public static void getMaxVal(int[] arr){
		int maxVal = arr[0];
		for(int i = 1; i < arr.length; i++){
			if(maxVal < arr[i]){
				maxVal = arr[i];
			}
		}
		System.out.println("the max val is " + maxVal);
	}
	
	//二维数组求和
	public static void getSumVal(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];
			}
		}
		System.out.println("the sum val is " + sum);
	}
}

  

posted @ 2018-11-19 10:27  SasaL  Views(119)  Comments(0Edit  收藏  举报