java - 在对象中传递 ArrayList

标签 java object arraylist

您好,我的程序有一个问题,它应该将学生信息存储到 Student 类型的对象中。存储的信息:姓氏、年级和投票。投票存储在 IntegerArrayList 中。每次我创建一个 Student 类型的新对象并将其添加到 Student 类型的 ArrayList 中(它存储学校的所有学生) ),它不断地将我输入的新投票添加到之前创建的 Student 对象中,该对象已存储在 ArrayList 中。

示例:我将一个 Student 添加到学生 ArrayList,输入 freddy、5b 和 123,然后检查学生 ArrayList它包含我已经添加的学生:freddy、5b 和 123。我添加了我在输入 josh 中给出的另一个学生、4t 和 1234 我检查

为什么它会修改已经创建并存储在ArrayList中的对象?我该如何修复它?

代码如下:

public class Student {
    private String lastname;
    private String grade; // example "4b" 
    private ArrayList<Integer> student_votes;  


    public Student(String lastname, String grade, ArrayList<Integer> student_votes) {
        this.lastname=lastname;
        this.grade=grade;
        this.student_votes=student_votes; 
    }

    public ArrayList getVotes() {
        return student_votes;
    }

    public String getLastname() {
        return lastname;
    }

    public String getGrade() {
        return grade;
    }

    public String toString() {
        return lastname+" "+grade+" "+getVotes();
    }

    public void print_student (Student student) {
        System.out.println(student);
    }

    public static void print_students(ArrayList<Student> students) {
        for(Student s : students) {
            System.out.print(s);
        }
        System.out.println("");
    }

    public static void menu() {
        System.out.println("\nPress 1 to add a student\nPress 2 to remove a student\nPress 3 to print the classroom\nPress 4 to exit");
    }

    public static void main(String[] args) {
        int choice, nv=0, i=0,average=0;
        Boolean exit=false;
        ArrayList<Student> students = new ArrayList<Student>();
        ArrayList<Integer> votes = new ArrayList<Integer>();
        String lastname = new String();
        String grade = new String();

        Scanner sc = new Scanner(System.in);
        Scanner st = new Scanner(System.in);
        do { 
            menu();
            choice=sc.nextInt();

            switch (choice) {
                case 1:  System.out.println("Enter your lastname:");
                         lastname=st.nextLine();   
                         System.out.println("Enter your grade:");
                         grade=st.nextLine();

                         System.out.println("Enter the amount of votes");
                         nv=sc.nextInt();



                         for(i=0;i<nv;i++) {
                             System.out.println("Enter vote n:"+(i+1));                 
                             votes.add(sc.nextInt());
                         }

                         students.add(new Student(lastname,grade,votes));
                         System.out.println("student added!");

                         break;

                case 2:  System.out.println("Enter student position: ");
                         nv = sc.nextInt();
                         students.remove(nv-1);
                         break;

                case 3:  print_students(students);
                         break;

                case 4: exit = true;

            }
        } while (exit==false);
    }
}

最佳答案

您为每个 Student 使用相同的 ArrayList 对象。 Student 不包含列表的副本,而是包含指向同一列表的指针。如果您不断通过任何一个指针向列表添加内容,所有此类指针都会受到影响。

您应该为每个学生创建一个新的ArrayList并将其传递到构造函数中。

关于java - 在对象中传递 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39562661/

相关文章:

javascript - 对象未被读取 node.js

java - 同步 arrayList- 不兼容的类型

java - JSP 自定义标签库 : Nested Evaluation

java - BouncyCaSTLe 属性 messageDigest 验证失败

java - 按下还原时 JFrame 非常小

oracle - PL/SQL用于自定义类型上的内爆功能

Javascript 对象初始化,最好的方法是什么?

java - SWIG - Java 代理类数组参数

java - 关于分配 List.element 的错误

java - ArrayList返回类型方法返回ArrayList变量的内存地址还是它的副本?