Method 1:
Public classtest0920{ Public Static voidSwapint[] arr,intIintj) { inttemp=Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; } Public Static voidFindmax2values (int[] arr,intLowintHighintX1,intX2) {//find the maximum and 2nd-largest element values in the array in the interval [Low,high] for(intXx:arr) {System.out.println (XX); } if(Arr[x1=low]<arr[x2=low+1])//X1=low is called an assignment expression, and the value of an assignment expression is the value of the leftmost variable. swap (ARR,X1,X2); for(intI=low+2;ii) {if(arr[i]>arr[x2])if(Arr[x1]<arr[x2=i])//very ingenious, in conjunction with the SWAP functionswap (ARR,X1,X2); } System.out.println ("Max2 Values:"); System.out.println (arr[x1]); System.out.println (arr[x2]); } Public Static voidMain (string[] args) {int[] Aa=New int[]{98,75,34,54,3900,38,18,20,9,23}; intX1=0; intX2=0; Findmax2values (AA,0, AA.LENGTH,X1,X2); } }
max2--solving two large element values in an array