打印列表返回奇怪的内存引用(Printing out list returns weird memory reference)

编程入门 行业动态 更新时间:2024-10-26 20:34:48
打印列表返回奇怪的内存引用(Printing out list returns weird memory reference)

所以我正在写一个简单的程序来排序列表。 我想在实际执行比较器/排序之前测试它,并且在打印出列表内容时我在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中的强项。 你可以说,我还是比较新的。 提前感谢您提供的任何帮助。

So I'm writing a simple program that sorts lists. I wanted to test it before I actually went into implementing the comparators/sort and I'm having an issue at the main method when it comes to printing out the contents of the list. Here is my tester:

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); } }

}

and my output looks like:

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

So I'm assuming its using the address of the entire package and referring to the specific memory address where the student information is stored....How exactly do I get around this? I imagine it should be pretty simple, but all of the various nuances of syntax aren't my strong suit in Java quite yet. As you can tell, I'm still relatively new at this. Thanks in advance for any help you can offer.

最满意答案

你告诉它打印出一个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()方法,以返回格式化的字符串,如何在Java中正确覆盖toString()?

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

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

有什么理由不使用这个吗?

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

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

You are telling it to print out a Student object, not a string. So the output is up to however that class implements the ToString() method (or inherits the default).

You can either print the individual properties and format however you like:

// 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); }

...or, you can override the ToString() method on your Student class to return a string formatted how you want: How to override toString() properly in Java?

Also, not sure if it matters, but regarding this line:

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

Any reason not to just use this?

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

...so that it's strongly typed. The previous version stores as ArrayList of Object instead of List of Student.

更多推荐

本文发布于:2023-08-07 10:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇怪   内存   列表   Printing   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!