public class Are {
public static void main(String[] args) {
int[] arr = { 8,7,6,5,4,3,2,1 };
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
int secondMax = arr[1];
for (int j = 0; j < arr.length; j++) {
if (arr[j] > secondMax && arr[j] < max) {
secondMax = arr[j];
}
}
System.out.println("最大值:" + max + ",第二大数" + secondMax);
}
}
本文介绍了一种在Java中寻找数组中最大值和第二大值的方法。通过两次遍历数组,首先找出最大值,然后在剩余元素中找出最大的元素作为第二大的值。这种方法简单直接,易于理解。
1594

被折叠的 条评论
为什么被折叠?



