I have two classes Student and ClassRoom, each class has an ArrayList. I'm trying to move a student object from students ArrayList to ClassRoom ArrayList but it is not working.
ClassRoom class code:
public class ClassRoom{
private String id;
private String name;
private int age;
ArrayList< ClassRoom >classRoom;
public ClassRoom(String id, String name, int age){
this.id = id;
this.name = name;
this.age = age;
}
public ClassRoom() {
classRoom = new ArrayList();
id = " ";
name = " ";
age = 0;
}
public void Add(String id,String name,int age){
ClassRoom c = new ClassRoom(id, name, age);
classRoom.add(c);
}
Student class code:
public class Student{
private String id;
private String name;
private int age;
private ClassRoom classroom;
ArrayList< Student >students;
public Student(String id, String name, int age){
this.id = id;
this.name = name;
this.age = age;
}
public Student() {
students = new ArrayList();
id = " ";
name = " ";
age = 0;
}
public void Add(String id,String name,int age){
Student s = new Student(id, name, age);
students.add(s);
}
public void MoveToClassRoom(String id, String name, int age){
for(int i=0; i< students.size();i++){
if (students.get(i).getId().equals(id)){
classroom.Add(students.get(i).getId(), students.get(i).getName(), students.get(i).getAge());
students.remove(i);
}
}
}
Studentto have anArrayList<Student>as a member? It seems like justid,name, andagewould be the good members ofStudent. Similarly, shouldClassRoomhave anArrayList<ClassRoom>? It seems likeClassRoomshould only need anArrayList<Student>and not haveid,name, andage. I know this isn't really answering your exact question but if these are issues it might make things less confusing if you fix them first.