【问题标题】:ArrayList from a method with a counter++ variable来自具有 counter++ 变量的方法的 ArrayList
【发布时间】:2021-05-06 06:55:19
【问题描述】:

我需要调用方法student,创建一个学生ID,该学生ID从1开始并随着每个新学生的增加而增加,然后将该新学生添加到ArrayList。但是,我的列表只获取最后一次调用的 ID 号并将其提供给列表的所有成员。如果我输入 GPA 3.4 的“James”和 GPA 2.9 的“Jack”,它会返回两个学生的相同学生 ID 为 2。我需要帮助才能让 James 的 ID 为 1,Jack 的 ID 为 2,等等。第一个块来自我的main 方法,最后的第二个块来自类本身。

ArrayList list1 = new ArrayList();
int option = -1;
String name;
double gpa;
Scanner studentInput = new Scanner(System.in);
Scanner gpaInput = new Scanner(System.in);
Scanner optionInput = new Scanner(System.in);
while (option != 0) {
    System.out.println("Please choose an option:");
    System.out.println("0: quit");
    System.out.println("1: Add a student to your list");
    System.out.println("2: Display your student list");
    option = optionInput.nextInt();
    switch (option) {
    case 0:
        System.out.println("Have a good Day!");
        break;
    case 1:
        System.out.println("Please enter the students name.");
        name = studentInput.next();
        System.out.println("Please enter the students GPA.");
        gpa = gpaInput.nextDouble();
        Student studentA = new Student(name, gpa);
        list1.add(studentA);
        break;

    case 2:
        System.out.println("Here is your list so far.");
        for (int i = 0; i < list1.size(); i++) {
            System.out.println(list1.get(i));
        }
    }

}
public class Student implements Comparable {
    private static int studentID = 0;
    private String name;
    private double gpa;

    public Student( String name, double gpa) {
        studentID++;
        this.name = name;
        this.gpa = gpa;
    }

    public int getStudentID() {
        return studentID;
    }

    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }

    public String getName() {
        return name;
    }

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

    public double getGpa() {
        return gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }
}

【问题讨论】:

    标签: arraylist static-variables


    【解决方案1】:

    类中的studentID 字段在Student 的所有实例之间共享,因此如果其值发生变化,getStudentID 将为所有实例返回新值。要解决此问题,请为学生 ID 添加另一个非静态字段,然后将静态字段的值存储到您刚刚在构造函数中添加的字段中:

    public class Student implements Comparable {
        private static int globalStudentID;
        private int studentID;
        private String name;
        private double gpa;
    
        public Student( String name, double gpa) {
            globalStudentID++;
            this.studentID = globalStudentID;
            this.name = name;
            this.gpa = gpa;
        }
    
        public int getStudentID() {
            return studentID;
        }
    
        public void setStudentID(int studentID) {
            this.studentID = studentID;
        }
    
        // ...
    }
    

    【讨论】:

    • 谢谢你,这帮助了很多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多