打印出arrayList时是否为空值?

时间:2016-04-09 14:33:41

标签: java arraylist printing null tostring

您好我已经在我的一个类中创建了一个toString方法,可以在下面看到。 学生班:

package Practical5;

public class Student extends Person {

    //instance variables
    private static int MAX_MODULES = 6;
    private StudentMode modeOfStudy;
    private boolean studentLoan;
    private int numEnrolledModules;

    //constructor
    public Student(String name, String dob, Address address, StudentMode modeOfStudy, boolean studentLoan) {
        super(name, dob, address);
    this.modeOfStudy = modeOfStudy;
    this.studentLoan = studentLoan;
    this.numEnrolledModules = 0;
    }

    //accessors & mutators
    public StudentMode getMode() {
        return modeOfStudy;
    }

    public boolean isStudentLoan() {
        return studentLoan;
    }

    public int getNumEnrolledModules() {
        return numEnrolledModules;
    }

    public void setMode(StudentMode modeOfStudy) {
        this.modeOfStudy = modeOfStudy;
    }

    public void setStudentLoan(boolean studentLoan) {
        this.studentLoan = studentLoan;
    }

    public void setNumEnrolledModules(int numEnrolledModules) {
        this.numEnrolledModules = numEnrolledModules;
    }

    @Override
    public void purchaseParkingPass() {
        System.out.println(getName() + " just purchased a parking pass with student discount.");
    }

    @Override
    public void addModule(String moduleCode) {
        if (getNumEnrolledModules() < MAX_MODULES) {
            System.out.println(getName() + " successfully registered for the module: " + moduleCode);
        }
        else {
            System.out.println("You are unable to register for " + moduleCode + " as the maximum number of permitted module enrolments has been reached.");
        }

    }

    public String toString() {
        return "Student [ ID: " + getId() + "; Name: " + getName() + 
                "; DOB: " + getDob() + "; Study Mode: " + getMode() +
                "; Number of Enrolled Modules: " + getNumEnrolledModules();
    }
}

人类:

package Practical5;

public abstract class Person {


    //instance variables
    private static int LAST_ID = 1000 + 1;
    private int id;
    private String name;
    private String dob;
    private Address address;

    //constructor
    public Person(String name, String dob, Address address) {
        super();
        LAST_ID ++;
        this.id = LAST_ID;

    }

    //accessors & mutators
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDob() {
        return dob;
    }

    public Address getAddress() {
        return address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    //methods
    public abstract void purchaseParkingPass();
    public abstract void addModule(String moduleCode);



}

然后我创建了一个测试器类并创建了一个新的ArrayList并将这些元素添加到它中。

然后我创建了一个for循环,以循环遍历每个元素并调用toString方法打印出每个元素的细节,但它返回空值。

测试人员类:     包Practical5;

import java.util.ArrayList;
import java.util.Scanner;



public class UIS_Tester {

    public static void main(String[] args) {

        Student student1 = new Student("James Black", "07/09/1995" , new Address("Wheeler's Road",10,"Belfast", "BT12 5EG", "Co.Antrim"),StudentMode.Fulltime, false);
        Student student2 = new Student("Adam Smith", "12/11/1979" , new Address("Ivy Hill",67,"Belfast", "BT17 7BN", "Co.Antrim"),StudentMode.Parttime, true);

        ArrayList<Person> uniPeople = new ArrayList<Person>();

        uniPeople.add(student1);
        uniPeople.add(student2);

        printMenu(uniPeople);



    }


    public static void printAllDetails(ArrayList<Person> uniPeople) {
        for (int i = 0; i < uniPeople.size(); i++) {
            System.out.println(uniPeople.get(i).toString());
        }
    }
}

输出:

Student [ ID: 1002; Name: null; DOB: null; Study Mode: Fulltime; Number of Enrolled Modules: 0
Student [ ID: 1003; Name: null; DOB: null; Study Mode: Parttime; Number of Enrolled Modules: 0

任何人都可以帮我解决这个问题吗?感谢

3 个答案:

答案 0 :(得分:2)

public Person(String name, String dob, Address address) {
    super();
    LAST_ID ++;
    this.id = LAST_ID;
}

构造函数完全忽略了它的三个参数。它不会将它们分配给相应的字段,因此这些字段保留其默认值:null。

答案 1 :(得分:1)

您必须在构造函数中存储名称值。您的版本未使用名称值。

public Person(String name, String dob, Address address) {
    super();
    this.name = name; // <== important line
    this.dob = dob;  // <== important line
    this.address = address;  // <== important line
    LAST_ID ++;
    this.id = LAST_ID;

}

答案 2 :(得分:0)

亲自和在学生中查看构造函数,应该使用方法头中的参数。

super(name,dob,address)