只有最后一个Object被添加到ArrayList中

时间:2014-01-03 06:39:01

标签: java arrays object arraylist

我创建了一个用户定义的数据类型并从文件中读取数据。以下是代码:

学生班:

package system.data;

public class Student {

private String firstName;
private String lastName;
private String regNumber;
private int coursework1Marks;
private int coursework2Marks;
private int finalExamMarks;
private double totalMarks;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getRegNumber() {
    return regNumber;
}

public void setRegNumber(String regNumber) {
    this.regNumber = regNumber;
}

public int getCoursework1Marks() {
    return coursework1Marks;
}

public void setCoursework1Marks(int coursework1Marks) {
    this.coursework1Marks = coursework1Marks;
}

public int getCoursework2Marks() {
    return coursework2Marks;
}

public void setCoursework2Marks(int coursework2Marks) {
    this.coursework2Marks = coursework2Marks;
}

public int getFinalExamMarks() {
    return finalExamMarks;
}

public void setFinalExamMarks(int finalExamMarks) {
    this.finalExamMarks = finalExamMarks;
}

public double getTotalMarks() {
    totalMarks = (coursework1Marks * 0.2) + (coursework2Marks * 0.2) + (finalExamMarks * 0.6);
    return totalMarks;
}

}

主要编码:

public class MainInterface extends javax.swing.JFrame {

private File studentFile = new File(".\\StudentMarks.txt");
private PrintWriter printWriter = null;
private FileOutputStream fileOutputStream = null;
public ArrayList<Student> studentDetails = null;
private Scanner input = null;
private int counter = 0;

/**
 * Creates new form MainInterface
 */
public MainInterface() {
    initComponents();
    setLocationRelativeTo(null);
    studentDetails = new ArrayList<Student>();
    ReadStudentDetails(studentDetails);
}
private void ReadStudentDetails(ArrayList<Student> studentDetails) {
    ArrayList<String> strList = new ArrayList<>();
    Student student = new Student();

    try {
        input = new Scanner(studentFile);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MainInterface.class.getName()).log(Level.SEVERE, null, ex);
    }

    while(input.hasNext()){
        counter++;
        String str = input.nextLine();
        strList.add(str);
        System.out.println(counter);
    }

    for (String item : strList) {
        int x = 0;
        String[] arr = item.split(":");

        student.setFirstName(arr[0]);
        student.setLastName(arr[1]);
        student.setRegNumber(arr[2]);
        student.setCoursework1Marks(Integer.parseInt(arr[3]));
        student.setCoursework2Marks(Integer.parseInt(arr[4]));
        student.setFinalExamMarks(Integer.parseInt(arr[5]));

        studentDetails.add(student);
    }

}

没有给出语法错误。但是当我尝试将ArrayList中的元素打印为

    for(Student item: studentDetails){
        System.out.println(item.getFirstName());
    }

它只发出文件中的最后一条记录(有3条记录)。为什么会这样?提前谢谢。

4 个答案:

答案 0 :(得分:6)

重复使用相同的学生对象,因为Java会将调用中的对象的引用传递给列表。换句话说,每次都会传递原始学生。解决方案是为每次通话创建一个新学生。

for (String item : strList) {
    int x = 0;
    String[] arr = item.split(":");

    Student student = new Student();

    student.setFirstName(arr[0]);
    student.setLastName(arr[1]);
    student.setRegNumber(arr[2]);
    student.setCoursework1Marks(Integer.parseInt(arr[3]));
    student.setCoursework2Marks(Integer.parseInt(arr[4]));
    student.setFinalExamMarks(Integer.parseInt(arr[5]));

    studentDetails.add(student);
}

答案 1 :(得分:1)

Student循环

中创建for对象的新实例 像这样

for (String item : strList) {
        int x = 0;
        String[] arr = item.split(":");
        Student student = new Student();
        student.setFirstName(arr[0]);
        student.setLastName(arr[1]);
        student.setRegNumber(arr[2]);
        student.setCoursework1Marks(Integer.parseInt(arr[3]));
        student.setCoursework2Marks(Integer.parseInt(arr[4]));
        student.setFinalExamMarks(Integer.parseInt(arr[5]));

        studentDetails.add(student);
    }

答案 2 :(得分:1)

实际上你总是使用同一个学生对象。你必须把学生=新学生();在for循环中。

答案 3 :(得分:1)

尝试在for循环中移动Student student = new Student();

 for (String item : strList) {
    int x = 0;
    String[] arr = item.split(":");
    Student student = new Student();
    student.setFirstName(arr[0]);
    student.setLastName(arr[1]);
    student.setRegNumber(arr[2]);
    student.setCoursework1Marks(Integer.parseInt(arr[3]));
    student.setCoursework2Marks(Integer.parseInt(arr[4]));
    student.setFinalExamMarks(Integer.parseInt(arr[5]));

    studentDetails.add(student);
}

希望这会有所帮助。