简体   繁体   中英

How to add an object from an ArrayList to another ArrayList in another class

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);
                }
    }
}

Students are not kinds of Classroom . Because you're using a couple of ArrayList s that are specific to both kinds, there's little interchangability. The reason for this is because every object that resides in a List must somewhere along the line share the same chain of inheritance that matches the List 's type.

In Java, every class you create implicitly inherits from a type Object . This is where the toString() and hashCode() methods comes from, for example. So if you wanted to put Student and Classroom in the same list, you could place them in a List of type <Object> ... but there's nothing too interesting you'd be able to do with that. It would however highlight the point that you'd be able to perform types of interactions with elements of that list that are generic enough to support all contained elements; ie you could call Object methods on any element within the List .

So, maybe you'd be interested in a slightly different design pattern, if you wanted to do something more application-specific with it's contents. If you want to place multiple kinds of objects within the same List , there must be some kind of shared functionality that you want; some case where it seems logical to treat a Student in the same way as you would a Classroom . If you wanted to do this, you could have both Student and Classroom extend a class of type SchoolEntity , which would contain methods and properties which both Student and Classroom elements would need. Then you could create a List of type <SchoolEntity> , to hold both kinds or even more.

For greater flexibility, you could define an interface ISchoolEntity , which both Student and Classroom could implement, and use a List<ISchoolEntity> instead. The reason this is more flexible is because Java doesn't support multiple inheritance ; this means that once Classroom and Student extend SchoolEntity , there'd be no way later they'd also be able to extend another class like CollegeEntity . Conversely, Java allows a class to implement many kinds of interface, so a Student and Classroom could be potentially be treated as both kinds of ISchoolEntity and ICollegeEntity , if the application required it later on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM