public class MainClass {
ArrayList<Man> mans = new ArrayList<Man>();
// I'm filling in this arraylist, but I'm not writing those parts because I want to summarize the
//code.
}
public class Man {
int index_father, index_son;
String name;
public Man(String name){
this.name = name;
}
}
Some guys in this community of men have a father-son relationship. My question is that:
I'm looking for a neater way to build this father-son relationship.
I tried to establish this relationship with integers (example: which_father shows the directory where the man's father is in the 'mans' list) but this has some disadvantages:
1-When creating objects, I always have to create sons after fathers. because:
private void createMan() {
Man man = new Man("Ethan");
man.index_father = 4;
mans.add(man);
mans.get(4).index_son = mans.indexOf(man);
}
If we haven't created the father yet, the 4th element of mans will not have been created yet.
2- In cases where I delete an element from the mans arraylist, which_fathers will not show the correct father man.
I may not know enough about Java, but there must be an easy way to establish a connection between objects (like the father-son relationship in this example), right?