打印列表返回奇怪的内存引用

时间:2015-11-08 01:40:33

标签: list sorting printing

所以我正在编写一个对列表进行排序的简单程序。我想在实际执行比较器/排序之前测试它,并且在打印出列表内容时我在main方法中遇到问题。这是我的测试员:

    public class Lab6Exercise {
public static void main(String[] args) {
    //creates list of students
    List<Student> studentList = new ArrayList<>();
    //randomizes GPA
    Random rand = new Random();
    int randID = rand.nextInt(10000) + 10000;
    int randInt = rand.nextInt(4);
    double randDec = rand.nextDouble();
    double randGrade = randInt + randDec;

    studentList.add(new Student(randID, "Chester", "Smith", randGrade));
    studentList.add(new Student(randID, "Sally", "Winters", randGrade));
    studentList.add(new Student(randID, "Tim", "Jackson", randGrade));
    studentList.add(new Student(randID, "Winston", "Pulitzer", randGrade));
    studentList.add(new Student(randID, "Jackie", "Harris", randGrade));
    studentList.add(new Student(randID, "Paul", "Newman", randGrade));
    studentList.add(new Student(randID, "Sally", "Ride", randGrade));
    studentList.add(new Student(randID, "John", "Smith", randGrade));
    studentList.add(new Student(randID, "Cassie", "Anderson", randGrade));
    studentList.add(new Student(randID, "Sam", "Bowman", randGrade));
    studentList.add(new Student(randID, "Wade", "Mathers", randGrade));
    studentList.add(new Student(randID, "Jackson", "Pink", randGrade));
    studentList.add(new Student(randID, "Bill", "Throwers", randGrade));
    studentList.add(new Student(randID, "Tupac", "Shakur", randGrade));
    studentList.add(new Student(randID, "Amy", "Allen", randGrade));
    studentList.add(new Student(randID, "Charlie", "Waffles", randGrade));
    studentList.add(new Student(randID, "Cindy", "Decker", randGrade));
    studentList.add(new Student(randID, "Douglas", "Harris", randGrade));
    studentList.add(new Student(randID, "Jimmy", "Duggers", randGrade));
    studentList.add(new Student(randID, "Vince", "Eisel", randGrade));


    System.out.println("STUDENT LIST BEFORE SORTING: \n");

    for (Student stud : studentList){
        System.out.println(stud);
    }
}

}

我的输出如下:

STUDENT LIST BEFORE SORTING: 

edu.csu.lab6.Student@677327b6
edu.csu.lab6.Student@14ae5a5
edu.csu.lab6.Student@7f31245a
edu.csu.lab6.Student@6d6f6e28
edu.csu.lab6.Student@135fbaa4
edu.csu.lab6.Student@45ee12a7
edu.csu.lab6.Student@330bedb4
edu.csu.lab6.Student@2503dbd3
edu.csu.lab6.Student@4b67cf4d
edu.csu.lab6.Student@7ea987ac
edu.csu.lab6.Student@12a3a380
edu.csu.lab6.Student@29453f44
edu.csu.lab6.Student@5cad8086
edu.csu.lab6.Student@6e0be858
edu.csu.lab6.Student@61bbe9ba
edu.csu.lab6.Student@610455d6
edu.csu.lab6.Student@511d50c0
edu.csu.lab6.Student@60e53b93
edu.csu.lab6.Student@5e2de80c
edu.csu.lab6.Student@1d44bcfa

Process finished with exit code 0

所以我假设它使用整个包的地址并参考存储学生信息的特定内存地址....我究竟如何解决这个问题?我想它应该很简单,但语法的各种细微差别还不是我在Java中的强项。你可以说,我还是比较新的。提前感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

你告诉它打印出一个Student对象,而不是一个字符串。因此输出结果是该类实现ToString()方法(或继承默认值)。

您可以根据需要打印各个属性和格式:

// You don't show the actual property names in your code so I made them up
for (Student stud : studentList) {
    System.out.printf("ID: %s, Name: %s %s, Grade: %s",
        stud.ID, stud.First, stud.Last, stud.Grade);
}

...或者,您可以覆盖Student类的ToString()方法,以返回格式化的字符串:How to override toString() properly in Java?

另外,不确定这是否重要,但关于这一行:

List<Student> studentList = new ArrayList<>();

有什么理由不使用它?

List<Student> studentList = new List<Student>;

...这样它就是强类型的。以前的版本存储为Object的ArrayList而不是Student of List。