这是我想的,,只用一次循环,效率可能会更高点- void getSecondMax(){
-
- int []arr={5500,78,1000,2000,79,80,-34,-45,-23,30,-29,-3,9,8,-12};
- int max=arr[0];
- int nextmax=arr[1];
- for(int i=0;i<arr.length;i++){
-
- if(max<arr[i]){
- nextmax=max;
- max=arr[i];
- }
-
- if(nextmax<arr[i]&&arr[i]<max){
-
- nextmax=arr[i];
- }
-
-
- }
-
- System.out.println(nextmax);
-
- }
复制代码 |