Printing out all the objects in array list
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
How to print object content in correct way?
号
我需要能够打印出数组列表中的学生对象(所有变量)。这有可能吗?当我尝试打印时,它会输出这种东西,例如
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | package student; public class Student { private String studentName; private String studentNo; private String email; private int year; public Student() { this.studentName = null; this.studentNo = null; this.email = null; this.year = -1; } public Student(String nName, String nNum, String nEmail, int nYr) { this.studentName = nName; this.studentNo = nNum; this.email = nEmail; this.year = nYr; } public void setStudentName(String newStudentName) { this.studentName = newStudentName; } public void setStudentNo(String newStudentNo) { this.studentNo = newStudentNo; } public void setEmail(String newEmail) { this.email = newEmail; } public void setYear(int newYear) { this.year = newYear; } public String getStudentName() { return studentName; } public String getStudentNo() { return studentNo; } public String getEmail() { return email; } public int getYear() { return year; } } package student; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class studentTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); List<Student> Students = new ArrayList(); Student student1 = new Student(); student1.setStudentName("Bob Marley"); student1.setStudentNo("N0002"); student1.setEmail("[email protected]"); student1.setYear(2); Students.add(student1); Student student2 = new Student(); student2.setStudentName("Bill Harvey"); student2.setStudentNo("N0003"); student2.setEmail("[email protected]"); student2.setYear(2); Students.add(student2); Student student3 = new Student(); student3.setStudentName("John Beans"); student3.setStudentNo("N0004"); student3.setEmail("[email protected]"); student3.setYear(2); Students.add(student3); System.out.println("Add new students:"); System.out.println("Enter number of students to add:"); int countStudents = input.nextInt(); for (int i = 0; i < countStudents; i++) { Student newStudents = new Student(); System.out.println("Enter details for student:" + (i + 1)); System.out.println("Enter name:"); newStudents.setStudentName(input.next()); System.out.println("Enter Number:"); newStudents.setStudentNo(input.next());System.out.println("Search by student number:"); System.out.println("Enter email:"); newStudents.setEmail(input.next()); System.out.println("Enter year:"); newStudents.setYear(input.nextInt()); Students.add(newStudents); } } } |
每当打印类的任何实例时,都会调用
因此,在student.student@82701E中,您将得到输出->
student.Student 为Type ,且82701e 是Hashcode 。
因此,您需要重写
1 2 3 4 5 | @Override public String toString() { return"Student No:" + this.getStudentNo() + ", Student Name:" + this.getStudentName(); } |
号
因此,当您从您的
1 2 3 4 5 6 7 8 9 |
在上述两种情况下,将调用
重写
1 2 3 4 5 6 7 | @Override public String toString() { return ("StudentName:"+this.getStudentName()+ " Student No:"+ this.getStudentNo() + " Email:"+ this.getEmail() + " Year :" + this.getYear()); } |
。
你必须在你的
1 2 3 |