如题:
/**
* 求数组中的第二最大数
* @author gaozhy
* @describe
* @date 2020/9/11 5:23 下午
*/
public class SecondMaxNumber {
public static void main(String[] args) {
int[] arr = new int[]{10, 1, 5, 14, 3, 0, 13, 18, 9,15,20};
int firstMax = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > firstMax) {
secondMax = firstMax;
firstMax = arr[i];
} else if (arr[i] > secondMax && arr[i] < firstMax) {
secondMax = arr[i];
}
}
// 数组中的第二最大数:18
System.out.println("数组中的第二最大数:"+secondMax);
}
}