java - 对学生记录的并行数组进行排序
问题描述
我需要帮助来完成我们班上的给定作业。我们被分配了一项任务,我们应该用学生的身份证、名字、姓氏、课程、年级、我们的预科成绩、期中成绩、期末成绩、暂定和期末成绩来制作学生记录。问题围绕着我应该用来以并行顺序对以下数组中的元素进行排序的算法。这是我的代码的一部分。
System.out.print("Number of students to record? ");
count = Integer.parseInt(keyboard.nextLine());
id = new String[count];
names = new String[count];
course = new String[count];
yearLevel = new int[count];
pGrade = new byte[count];
mGrade = new byte[count];
tFGrade = new byte[count];
fGrade = new byte[count];`
showData(id, names, course, yearLevel, pGrade, mGrade, tFGrade, fGrade); // Invoke the method for displaying the array elements
// Show the students in sorted order
System.out.println("Sorted Data");
showData(id, names, course, yearLevel, pGrade, mGrade, tFGrade, fGrade);
}
public static void populateArrays(String[] id, String[] n, String[] c, int[] y, byte[] p, byte[] m, byte[] t, byte[] f) {
for (int index = 0; index < n.length; index++) {
System.out.print("Enter the id number of student " + (index + 1) + ":");
id[index] = keyboard.nextLine();
System.out.print("Enter the name of student " + (index + 1) + ":");
n[index] = keyboard.nextLine();
System.out.print("Enter the course of student " + (index + 1) + ":");
c[index] = keyboard.nextLine();
System.out.print("Enter the year level of student " + (index + 1) + ":");
y[index] = Integer.parseInt(keyboard.nextLine());
System.out.print("Enter the prelim grade of student " + (index + 1) + ":");
p[index] = Byte.parseByte(keyboard.nextLine());
System.out.print("Enter the midterm grade of student " + (index + 1) + ":");
m[index] = Byte.parseByte(keyboard.nextLine());
System.out.print("Enter the tentative final grade of student " + (index + 1) + ":");
t[index] = Byte.parseByte(keyboard.nextLine());
// compute the final grade of student as the average of prelim, midterm and tentative final grade
f[index] = (byte) ((1.0 * p[index] + 1.0 * m[index] + 1.0 * t[index]) / 3.0 + 0.5);
}
return;
}
这是我需要帮助的部分。我不知道应该使用什么算法对数组元素进行排序。如果是这样,我应该如何将所有 8 个数组放在一个排序算法中?这是方法。
public static void sortDataBasedOnNames(String[] id, String[] n, String[] c, int[] yLevel, byte[] p, byte[] m, byte[] t,
byte[] f) {
} // end of sortBasedOnNames method
任何帮助或建议将不胜感激。正如你所知道的,我对此完全陌生,并且真的尽我最大的努力来解决如何解决这个算法。
解决方案
我认为通过说“并行排序”数组,您指的是按照一定的约束将它们全部排序,而不是“并行编程排序方法”,因为您已经说过您是新手。
问题是您必须选择一个字段来开始对所有数组进行排序。此外,您只需执行以下步骤即可轻松解决问题:
- 创建班级:学生
- 插入所有学生的数据
- 调用排序方法(基于特定字段)。
例如,如果要根据学生的 ID 对数组进行排序,只需在排序方法中指定即可。
为了实现第 3 步,您可以执行以下操作:
- 实现一个比较器并将您的数组与比较器一起传递给将其作为第二个参数的排序方法。
- 在您的对象所在的类中实现 Comparable 接口,并将您的数组传递给仅采用一个参数的排序方法。
否则,您可以使用“lambda 表达式”来做到这一点。lambda 表达式是一小段代码,它接受参数并返回一个值。Lambda 表达式类似于方法,但它们不需要名称,并且可以直接在方法体中实现。
我将为您附上一些有用的链接,您还可以在其中找到执行此操作的代码:使用越来越多的方法:
推荐阅读
- string - 大写和小写的所有可能性
- pointers - 结构指针解引用
- python - 如何通过散景图中的选择更改来替换面板的一部分?
- c++ - 如何使用 swig 将 const 字符串参数从 perl 传递到 c++
- json - 批量更新 PostgreSQL 中的 JSON 字段
- flutter - 带有列和行的颤振座椅视图
- c# - Jenkins 构建失败并出现错误“未安装文件扩展名 '.tif' 的 IFilter”但在本地工作
- objective-c - 在目标c的tableview中加载时重复选择单元格
- python - 未安装来自自定义通道的 Conda 依赖项
- ios - 您可以从 PDF 数据创建 UIImage 吗?