数组最大值(java)

public class MaxElementOfArray {

	/**@author Bruce
	 * @param args
	 */
	public static void main(String[] args) {
		int[] arr = new int[10];
		for(int i = 0;i<arr.length;i++){
			arr[i] = (int) (Math.random()*100);
			System.out.println(arr[i]);
		}
		
		int res = Max(arr,arr.length);
		System.out.println("max = " +res);
	}
	
	public static int Max(int[] arr,int length)
	{
		int max1 = 0;
		int max2 = 0;
		
		// stop the recursion if condition expression
		if(length == 1)
			return arr[0];
		else
		{
			//else continue recursion
			max1 = Max(arr,length-1);
			if(max1 > arr[length-1])
				max2 = max1;
			else
				max2 = arr[length-1];
			
			return max2;	           
		}
	}
}

你可能感兴趣的:(java,递归,数组最大值)