gpt4 book ai didi

java - Print Treeset 返回奇怪的打印结果?

转载 作者:行者123 更新时间:2023-11-30 01:41:31 24 4
gpt4 key购买 nike

所以我有三个类(class):

  • 实验室
  • 学生
  • 考试

我需要输入学生的 ID,将 Exam 类的考试添加到具有 Exam 类型的 Treeset 中,所有这些都在 Student 类中。

完成此操作后,我需要使用迭代器打印此 Treeset..但我得到的只是一些奇怪的: java.util.TreeMap$KeyIterator@1b6d3586 相反.

我创建了迭代器并传递了 Treeset,但代码不起作用。

这里是文件。

Lab.java

public class Lab {

public static void main(String[] args) {

Student stu = new Student();

Student.insert();
System.out.println("Insert ended.\n");

stu.print();
System.out.println("\nEnd of the program.");

}

}

Student.java

import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Student implements Comparable<Student>{

private static int ID;

private static Set<Exam> exams = new TreeSet<Exam>();

public static void insert() {

Scanner input = new Scanner(System.in);
Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
Scanner input3 = new Scanner(System.in);

int exam_code = 1;
String exam_name;
int exam_grade;

System.out.println("Student's ID: ");
ID = input.nextInt();

System.out.println("--- EXAMS INSERT ---");

while (exam_code != 0) {

System.out.println("Exam's code: ");
exam_code = input1.nextInt();

if (exam_code == 0) {
break;
}

else {

System.out.println("Exam's name: ");
exam_name = input2.nextLine();

System.out.println("Exam's grade: ");
exam_grade = input3.nextInt();

exams.add(new Exam(exam_code, exam_name, exam_grade));

}

}

input.close();
input1.close();
input2.close();
input3.close();

}

public void print() {

System.out.println("ID: " + ID);

System.out.println("\nExams done: ");

Iterator<Exam> es = exams.iterator();

boolean hasnext = false;

while (hasnext != true) {

if (es.hasNext()) {

System.out.println(es);

hasnext = true;
}

else {
hasnext = false;
}
}

}

@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
return 0;
}

}

考试.java


public class Exam implements Comparable<Exam>{

public int exam_code;
private String exam_name;
private int exam_grade;

public Exam(int code, String name, int grade) {

exam_code = code;
exam_name = name;
exam_grade = grade;

}

@Override
public int compareTo(Exam o) {
// TODO Auto-generated method stub
return 0;
}

}

最佳答案

替换

System.out.println(es);

System.out.println(es.next());

编辑:最好将整个 print() 方法替换为

public void print() {

System.out.println("ID: " + ID);

System.out.println("\nExams done: ");

for (Exam exam : exams) {
System.out.println(exam);
}

}

因为不需要自己实现迭代,所以 Java 中有 for-each 循环来处理这种情况。

编辑:使用迭代器

public void print() {

System.out.println("ID: " + ID);

System.out.println("\nExams done: ");

Iterator<Exam> es = exams.iterator();

while (es.hasNext()) {
System.out.println(es.next());
}
}

关于java - Print Treeset<CustomClass> 返回奇怪的打印结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59778771/

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