int[] arr = {4,8,2,45,12,74,22};
SecondBig(arr);
public void SecondBig(
int[] arr){
int max1, max2;
max1 = max2 = arr[
0];
int v;
for (
int i =
1; i < arr.length; i++) {
v = arr[i];
if (v > max2) {
if (v > max1) {
max2 = max1;
max1 = v;
}
else {
max2 = v;
}
}
}
if(max1 == max2){
max2 = arr[
1];
for(
int i=
2;i<arr.length;i++) {
if(arr[i]>max2)
max2 =arr[i];
}
}
System.
out.println(
"max1=" + max1 +
",max2=" + max2);
}
参考 Java取一个数组里面第二大的数据