我目前正在尝试用 Java 对分数的多维数组进行排序。我能够创建表格并按常规顺序打印,但无法按升序打印。这是我的代码:
public class SortGrades {
/** Main method */
public static void main(String args[]) {
// Students' answers to the questions
char[][] answers = {
{'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
{'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'},
{'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'},
{'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'},
{'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
{'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
{'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
{'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};
// Key to the questions
char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};
// Create array for student # and correct answers
int[][] userScores = new int[8][2];
// Grade all answers
for (int i = 0; i < answers.length; i++) {
// Grade one student
int correctCount = 0;
for (int j = 0; j < answers[i].length; j++) {
if (answers[i][j] == keys[j]) {
correctCount++;
}
userScores[i][1] = correctCount;
}
}
for (int row = 0; row < answers.length; row++) {
int value = 0;
int index = -1;
userScores[row][0] = row;
for (int column = row; column < answers.length; column++) {
if (userScores[row][1] > userScores[column][0] || value ==
userScores[column][0]
&& userScores[index][1] >
userScores[column][1]) {
index = column;
value = userScores[row][1];
}
if (index == row) {
int temp0 = userScores[index][0];
int temp1 = userScores[index][1];
userScores[index][0] = userScores[row][0];
userScores[index][1] = userScores[row][1];
userScores[row][0] = temp0;
userScores[row][1] = temp1;
}
}
}
for (int row = 0; row < answers.length; row++) {
System.out.print("Student " + userScores[row][0] + " score is ");
for (int column = 0; column < answers.length; column++);
System.out.print(userScores[row][1] + "\n");
}
}
}
我对 Java 和计算机科学总体来说非常陌生,并且没有研究过几个人发布的高级方法。我试图完成这个练习,除了数组和排序的基础知识之外,不使用任何东西。
我尝试使用 if 语句创建一个 for 循环,如果第二列编号大于第二列编号,则该语句应该交换两对数据。例如我想要做什么:
我如何需要它: 学生 3 分数为 4 学生 2 分数为 5 学生 1 分数为 6 学生 0 分数为 7 学生 5 分数为 7 学生 6 分数为 7 学生 7 分数为 7 学生4分数是8
一种简单的方法是冒泡排序。
for (int i = 0; i < userScores.length - 1; i++) {
// the outer loop keeps track of each pass of going through the list
for (int j = 0; j < userScores.length - i - 1; j++) {
// the inner loop checks the elements next to each other in the list
// '-i' is used since, after every cycle of the outer loop,
// the final i elements are correctly arranged and don't require further checking.
// '-1' ensures that when comparing an element to its neighbor, we
// don't go out of bounds
if (userScores[j][1] > userScores[j + 1][1]) {
// if the current score is greater than the following score, they need to be swapped
// swap the two elements.
// (we swap the entire sub-arrays,
// to preserve the association student number and score)
int[] temp = userScores[j]; // store the current element
userScores[j] = userScores[j + 1]; // place the next element in the current position
userScores[j + 1] = temp; // put the saved element in the next position
}
}
}
然后你可以像这样打印排序后的分数
for (int i = 0; i < userScores.length; i++) {
System.out.println("Student " + userScores[i][0] + " score is " + userScores[i][1]);
}
排序是由双
for
循环处理的,我们比较彼此相邻的每对项目,如果它们的顺序错误(在本例中为升序),则交换它们。重复此操作,直到不再需要交换,这意味着二维数组已排序。条件 if (userScores[j][1] > userScores[j + 1][1])
确保我们查看每个子数组的第二个元素以确定是否需要交换。
这是一个简单的排序,首先将分数按升序排序。如果分数相等,则检查学生编号,以按学生编号的升序对具有该特定分数的学生进行排序。
for (int rowA = 0; rowA < answers.length-1; rowA++) {
for (int rowB = rowA+1; rowB < answers.length; rowB++) {
if (userScores[rowA][1] > userScores[rowB][1]) {
int[] temp = userScores[rowA];
userScores[rowA] = userScores[rowB];
userScores[rowB] = temp;
} else if (userScores[rowA][0] > userScores[rowB][0]) {
int[] temp = userScores[rowA];
userScores[rowA] = userScores[rowB];
userScores[rowB] = temp;
}
}
}
通过使用比较器,可以简化这一过程。
这表示对于大小为
2
的int数组,如果分数不相等,则对它们进行排序,否则,继续对学生id进行排序。
Comparator<int[]> comp = Comparator.<int[]>comparingInt(i -> i[1])
.thenComparing(i -> i[0]);
for (int rowA = 0; rowA < answers.length-1; rowA++) {
for (int rowB = rowA+1; rowB < answers.length; rowB++) {
if (comp.compare(userScores[rowA], userScores[rowB]) > 0) {
int[] temp = userScores[rowA];
userScores[rowA] = userScores[rowB];
userScores[rowB] = temp;
}
}
}
最后,这是最简单的。使用之前的比较器流式传输每个学生的分数和 ID。内部排序也更高效。
Arrays.stream(userScores).sorted(comp).toArray(int[]::new);
Integer[] sortedScores =
Arrays.stream(userScores).sorted(comp).toArray(Integer[]::new);