我是想把排序后数组的前3名成绩,赋值给新的数组。可是不知道数组之间怎么赋值。
yanping_0512
2014-06-06
2 回答
public static void main(String[] args) { int[] scores = { 89, -23, 64, 91, 119, 52, 73 }; int[] newScores = new int[3]; Arrays.sort(scores); int num = 0; int j=0; for (int i = scores.length - 1; i >= 0; i--) { if (scores[i] < 0 || scores[i] > 100) { continue; } num++; if (num > 3) { break; } newScores[j] = scores[i]; j++; } System.out.println("原数组:" + Arrays.toString(scores)); System.out.println("新的数组:" + Arrays.toString(newScores)); }
举报
0/150
提交
取消