1. 程式人生 > >java 陣列(二)

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);
	}
}