装载声明:http://blog.csdn.net/lxsmk9059/article/details/77920206?locationNum=1&fps=1
1 int array[] = {1,12,58,369,45,17,59,3654,370}; 2 int max = array[0]; 3 int secondmax = array[0]; 4 5 for(int i = 0; i < sizeof(array)/sizeof(int); i++) 6 { 7 if(array[i] > max) 8 { 9 secondmax = max; 10 max = array[i]; 11 } 12 else if(array[i] > secondmax) 13 { 14 secondmax = array[i]; 15 } 16 }
个人思考编辑扩展如下:
public static void main(String[] args) { int [] a= {1,2,33,22,442,23,54,66}; int max=a[0]; int second=a[0]; int thrid=a[0]; for(int i=1;i<a.length;i++) { if (a[i]>max) { second=max; max=a[i]; }else if(a[i]>second){//第二大的 thrid=second; second=a[i]; }else if(a[i]>thrid) { thrid=a[i]; } } System.out.println(max); System.out.println(second); System.out.println(thrid); }