如何找到数组中的第二大元素,这是我的代码,但我无法找出问题所在,非常感谢帮助
import java.util.Arrays;
import java.util.Random;
public class NDlargest
{
Random random = new Random();
private int[] array = new int[10];
private int largest;
private int largest2;
public NDlargest()
{
largest = 0;
largest2 = 0;
for (int i =0; i< array.length; i++)
{
array[i] = 1 + random.nextInt(100);
}
System.out.println(Arrays.toString(array));
}
public int getLargest()
{
for (int i = 0; i < array.length; i++)
{
int number = array[i];
if (number > largest)
{
largest = number;
}
}
return largest;
}
public int getSecondLargest()
{
for (int i = 0; i < array.length; i++)
{
int number2 = array[i];
if (largest2 < largest)
{
if (number2 > largest2)
{
largest2 = number2;
}
}
else
{
break;
}
}
return largest2;
}}