你好我这个程序有问题,它应该将学生信息存储到Student
类型的对象中。存储的信息:姓氏,成绩和投票。投票存储在ArrayList
Integer
中。每当我创建一个Student
类型的新对象并将其添加到ArrayList
类型Student
(它存储学校的所有学生)时,它会不断添加新的投票我输入之前创建的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);
}
}
答案 0 :(得分:1)
您为每个ArrayList
使用相同的Student
对象。 Student
不包含列表的副本,而是包含指向同一列表的指针。如果你继续通过任何一个指针添加到列表中,那么所有这些指针都会受到影响。
您应该为每个学生创建一个new ArrayList
并将其传递给构造函数。
答案 1 :(得分:1)
问题在于你输入投票的逻辑,你正在使用一个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();
}
答案 2 :(得分:0)
您使用相同的ArrayList为每个学生投票,每个学生都有相同的投票列表(当您将列表添加到student-1时,让我们说2票,然后您将列表添加到学生-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个就足够了。