看下面的代码:
public static void main(String args[]) { int i; int large[] = new int[5]; int array[] = { 33, 55, 13, 46, 87, 42, 10, 34, 43, 56 }; int max = 0, index; for (int j = 0; j < 5; j++) { max = array[0]; index = 0; for (i = 1; i < array.length; i++) { if (max < array[i]) { max = array[i]; index = i; } } large[j] = max; array[index] = Integer.MIN_VALUE; System.out.println("Largest " + j + " : " + large[j]); }}注意: 如果您不想更改输入的数组,请对其进行复制并在复制的数组上执行相同的操作。
看看Integer.MIN_VALUE。
我得到以下输出:
最大0:87
最大的1:56
最大2:55
最大的3:46
最大4:43



