标签:
方法1:
public class test0920{ public static void swap(int[] arr,int i,int j){ int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } public static void findMax2Values(int [] arr,int low,int high,int x1,int x2){//找出数组中区间[low,high)下最大和第2大的元素值 for (int xx :arr ) { System.out.println(xx); } if(arr[x1=low]<arr[x2=low+1])//x1=low称为赋值表达式,赋值表达式的值是最左侧变量的值。 swap(arr,x1,x2); for (int i=low+2;i<high ;++i ) { if(arr[i]>arr[x2]) if(arr[x1]<arr[x2=i])//很巧妙,在swap函数的配合下 swap(arr,x1,x2); } System.out.println("max2 values:"); System.out.println(arr[x1]); System.out.println(arr[x2]); } public static void main(String[] args) { int[] aa=new int[]{98,75,34,54,3900,38,18,20,9,23}; int x1=0; int x2=0; findMax2Values(aa,0,aa.length,x1,x2); } }
标签:
原文地址:http://www.cnblogs.com/lz3018/p/4824260.html