如何使用一次for循环得到数组中第二大的数和第三大的数

装载声明:http://blog.csdn.net/lxsmk9059/article/details/77920206?locationNum=1&fps=1

 int array[] = {,,,,,,,,};
int max = array[];
int secondmax = array[]; for(int i = ; i < sizeof(array)/sizeof(int); i++)
{
if(array[i] > max)
{
secondmax = max;
max = array[i];
}
else if(array[i] > secondmax)
{
secondmax = array[i];
}
}

个人思考编辑扩展如下:

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);
}
上一篇:Java-Class-C:org.springframework.util.Assert


下一篇:Solve Error: node postinstall sh: node: command not found