你好,我有这个程序的问题,它应该将学生信息存储到Student
类型的对象中。存储信息:姓氏,年级和选票。投票存储在Integer
的ArrayList
中。每当我创建一个Student
类型的新对象,并将其添加到类型为Student
(它存储学校的所有学生)的ArrayList
时,它会不断添加我输入的前一个Student
对象创建的新投票,存储在ArrayList
。在对象中传递ArrayList
例子:我添加Student
给学生ArrayList
,我给在输入弗雷迪,5b和123,然后我检查学生ArrayList
,它包含了我已经加入学生:我再添弗雷迪,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);
}
}
为什么有两个扫描仪?您只能使用一个,但仍可实现相同的目标。 –
您正在为每个学生设置相同的列表对象。如果你想要不同的内容,你需要构建一个单独的。 – shmosel
@shmosel所以我需要为每个学生创建一个数组列表?有没有办法避免这种情况? – BlueJay