How to find second largest number in an array in Java?

后端 未结 10 991
野的像风
野的像风 2021-01-01 05:57

I\'m just practicing some MIT java assignments. But, I\'m not sure how to find the second largest number. http://ocw.csail.mit.edu/f/13

  public class Marath         


        
10条回答
  •  别那么骄傲
    2021-01-01 06:24

        private void secondLargest(int arr[]){
    
        int maxOne=arr[0];
        int maxTwo=arr[1];
    
        for(int i=0;imaxOne){
    
                maxTwo=maxOne;
                maxOne=arr[i];
            }else if (arr[i]>maxTwo) {
    
                maxTwo=arr[i];
            }
    
        }
    
        System.out.println(maxOne);
        System.out.println(maxTwo);
    }
    

提交回复
热议问题