打印出数组列表中的所有对象

2022-09-04 05:38:52

我需要能够打印出数组列表中的Student对象(所有变量)。这可能吗?当我尝试打印时,它会输出这种东西,例如。我认为它是或什么student.Student@82701ehexadecimal

这是我的代码:

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("student2@student.com");
        student1.setYear(2);

        Students.add(student1);

        Student student2 = new Student();

        student2.setStudentName("Bill Harvey");
        student2.setStudentNo("N0003");
        student2.setEmail("student3@student.com");
        student2.setYear(2);

        Students.add(student2);

        Student student3 = new Student();

        student3.setStudentName("John Beans");
        student3.setStudentNo("N0004");
        student3.setEmail("student4@student.com");
        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);
        }


    }
}

答案 1

在类中重写方法,如下所示:toString()Student

   @Override
   public String toString() {
        return ("StudentName:"+this.getStudentName()+
                    " Student No: "+ this.getStudentNo() +
                    " Email: "+ this.getEmail() +
                    " Year : " + this.getYear());
   }

答案 2

每当您打印类的任何实例时,都会调用 class 的实现,该实现返回您正在获取的表示形式。它包含两个部分: - 和defaulttoStringObjectTypeHashcode

所以,在学生中。作为输出 -> 获得的Student@82701e

  • student.Student是 、 和Type
  • 82701eHashCode

因此,您需要重写类中的一个方法才能获得必需: -toStringStudentString representation

@Override
public String toString() {
    return "Student No: " + this.getStudentNo() + 
           ", Student Name: " + this.getStudentName();
}

因此,当您从类中打印您的 时,它将为每个实例调用该方法,而不是类中的方法:-mainArrayListtoStringoverridedObject

List<Student> students = new ArrayList();

// You can directly print your ArrayList
System.out.println(students); 

// Or, iterate through it to print each instance
for(Student student: students) {
    System.out.println(student);  // Will invoke overrided `toString()` method
}

在上述两种情况下,都将调用类中重写的方法,并打印每个实例的适当表示形式。toStringStudent