簡體   English   中英

在對象中傳遞ArrayList

[英]Passing ArrayList in a object

您好,我有這個程序的問題,它應該將學生信息存儲到Student類型的對象中。 存儲的信息:姓氏,等級和投票。 投票存儲在IntegerArrayList中。 每當我創建一個Student類型的新對象並將其添加到Student類型的ArrayList中(它存儲一所學校的所有學生)時,它都會不斷將輸入的新投票添加到創建的上一個Student對象中存儲在ArrayList

示例:我向學生ArrayList添加了一個Student ,輸入了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不包含列表的副本,而是指向同一列表的指針。 如果您繼續通過任一指針添加到列表中,則所有此類指針都會受到影響。

您應該為每個學生創建一個new ArrayList並將其傳遞給構造函數。

問題出在您輸入投票的邏輯上,您正在使用一個ArrayList收集將分配給任何學生的所有投票。

當您傳遞一個對象時,您不會復制該對象,而是給該對象提供了引用,這意味着,根據您的邏輯,所有學生在您的主菜單中對ArrayList的引用都將具有相同的引用。 通過為每個學生創建一個新的ArrayList可以輕松解決此問題:

System.out.println("Enter the amount of votes");
nv=sc.nextInt();
final ArrayList<Integer> votes = new ArrayList<>();
for(i=0;i<nv;i++)
{
    System.out.println("Enter vote n:"+(i+1));
    votes.add(sc.nextInt());
}

這將為您解決問題,但是我認為使用ArrayList確實沒有意義,因為您會知道投票的數量。 然后代碼將是這樣,但是需要修改Student以接受數組:

System.out.println("Enter the amount of votes");
nv=sc.nextInt();
final int[] votes = new int[nv];
for(i=0;i<nv;i++)
{
    System.out.println("Enter vote n:"+(i+1));
    votes[i] = sc.nextInt();
}

您對每個學生使用相同的ArrayList進行投票,每個學生都具有相同的投票和相同的投票列表(當您將列表添加到Student-1時,比如說2票,然后將列表添加到student-2時,具有3票,那么兩個學生現在將有5票),如下更改:

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

現在應該可以了。 另外,您使用的是2台不必要的掃描儀,其中1台就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM