gpt4 book ai didi

java - 尝试打印链接数组时,学生可能没有作业或有很多作业。 printStudentDetails 打印每个学生加上 x 个作业对象

转载 作者:行者123 更新时间:2023-12-02 03:02:57 25 4
gpt4 key购买 nike

就像在图书馆一样,学生可以借 n 本书。我想从两个数组“students[]”和“currentJobs[]”打印,因此输出针对每个学生 - ID1、姓名、job1、job2。 ID2、姓名(无职位)。 ID3,作业1。

我创建了一个嵌套的 for 循环,但不知道如何对链接进行编码。公共(public)字段是studentID,所以我需要外部循环来获取studentID并比较内部循环(数组)上的每个studentID并打印每个匹配的详细信息

 public void displayStudentsReport()
{
System.out.println("***** Display Students Report *****");
System.out.println();

for (int i = 0; i < indexCount; i++)
{
students[i].printStudentDetails();
// print job details for each student
for (int j = 0; j < indexNo; j++)
{
currentJobs[j].printJobDetails();
}
}
}

public void printStudentDetails()
{
System.out.println("Printing from Student class");
System.out.printf("%-25s%-40s\n", "Student ID: ", studentID);
System.out.printf("%-25s%-40s\n", "Name: ", name);
System.out.println();
}

public void printJobDetails()
{
System.out.printf("%-25s%-40s\n", "Job: ", jobTitle);
System.out.printf("%-25s%-4.1f", "Current Working Hours: ", totalHours);
System.out.println("hrs");
System.out.printf("%-25s%-40s\n", "Employer: ", employer);
System.out.println();
}

预期结果解释

最佳答案

只是对你所做的事情进行了一些修正。在第二个列表中,您应该检查它是否是列表第一个中的正确学生。

public void displayStudentsReport()
{
System.out.println("***** Display Students Report *****");
System.out.println();

for (int i = 0; i < students.length; i++)
{
String comp;
comp = students[i].getStudentID();
students[i].printStudentDetails();
// print job details for each student
boolean studentHasJob = false;
for (int j = 0; j < currentJobs.length; j++)
{
if(currentJobs[j].getStudentID().equalsIgnoreCase(comp)) {
studentHasJob = true;
currentJobs[j].printJobDetails();
}
}
if(!studentHasJob){
System.out.println("Student " + comp + " has no jobs");
}
}


}

如果您想在学生没有工作的情况下打印特定的内容,我添加了一个小 boolean 值。

希望这就是您正在寻找的!

---编辑

所以我再次检查了代码,因为我认为这里存在性能问题。事实上,在代码中您多次传递了 currentJobs 数组。正好是 Students.length * currentJobs.length 倍。这是很多。因此,这里是可以做得更好的预览:

private void displayStudentsReport() {
System.out.println("***** Display Students Report *****");
System.out.println();

// build map of <StudentId : List of job for this student>
// perf is much better here because you pass on the job list only once
// whereas before you passed (students.length * currentJobs.length) times
Map<String, List<Job>> jobByStudent = new HashMap<>();
for(Job currentJob : currentJobs) {
if(jobByStudent.containsKey(currentJob.getStudentID())) {
jobByStudent.get(currentJob.getStudentID()).add(currentJob);
} else {
List<Job> jobs = new ArrayList<>();
jobs.add(currentJob);
jobByStudent.put(currentJob.getStudentID(), jobs);
}
}

for(Student student : students) {
String comp = student.getStudentID();
student.printStudentDetails();

if(jobByStudent.containsKey(comp)) {
for(Job job : jobByStudent.get(comp)) {
job.printJobDetails();
}
} else {
System.out.println("Student " + comp + " has no jobs");
}
}
}

关于java - 尝试打印链接数组时,学生可能没有作业或有很多作业。 printStudentDetails 打印每个学生加上 x 个作业对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57039972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com