JAVA 采用递归方式实现求数组最大值
其它就不多说了,直接上代码吧
public class Test {
public static void main(String[] args) {
int numbers[] = new int[]{1, 5, 7777,9,20,122};
System.out.println(getMaxNumber(numbers));
}
private static int getMaxNumber(int[] numbers) {
return getMaxNumber(numbers,0);
}
private static int getMaxNumber(int[] numbers, int from) {
if (from == numbers.length){
return numbers[numbers.length-1];
}
return Math.max(numbers[from],getMaxNumber(numbers,from+1));
}
}
- –