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)
最新推荐文章于 2024-03-17 20:23:03 发布