Multidimensional Arraylist Java

I have several problems with this. I have one ArrayList. I searched for several days and I can not find the answer anywhere:

private List<FamilyTree> Tree;

I can add a new Treesin arraythe following way:

FamilyTree Generation = new FamilyTree();
Generation.add(new Tree());

I basically want to be able to move between generations. So, for example, I add a new Person to the tree

Generation.add(new Person(height, hair colour, eyes));

Then I decide, I want to add another person to the previous generation. That is, for ArrayListcontaining the current ArrayList(not this one).

I'm not sure that I am explaining my problem well, so here is a diagram:

----John----Peter----Sandra----Rachel-----
   /   \       |       |
-Jon--Sunny---Cassie--Milo---
                     /  |  \
                   Ron-Kim-Guy

, ArrayList , , . Arraylist(s). , , ??

+3
3

, ​​,

class Person {
   final Person mother, father;
   final List<Person> children = new ArrayList<>();

   public Person(Person mother, Person father) {
     this.mother = mother;
     this.father = father;
     mother.addChild(this);
     father.addChild(this);
   }

   public void addChild(Person p) {
     children.add(p);
   }
}

, -

for(Person p = ...; p != null; p = p.mother) {

}

, , , , .

+4

, . . .

- , , , .. .

, , .

+1

The easiest way is to have each link to a parent. Perhaps if you create a Person object similar to this:

public class Person{

ArrayList<Person> childs;//the child nods
Person parent; //the parent, null if is the root

}
+1
source

All Articles