Вроде бы все проверил, у каждого правильные дети
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
ArrayList<Human> children0 = new ArrayList<>();
ArrayList<Human> children = new ArrayList<>();
ArrayList<Human> children1 = new ArrayList<>();
ArrayList<Human> children2 = new ArrayList<>();
ArrayList<Human> Human = new ArrayList<>();
Human child1 = new Human("Sonya", 16, false,children0);
Human child2 = new Human("Kiril", 22, true,children0);
Human child3 = new Human("Sofia", 13, false,children0);
children.add(child1);
children.add(child2);
children.add(child3);
Human father = new Human("Oleg", 47, true, children);
Human mother = new Human("Olga", 43, false, children);
children1.add(father);
children2.add(mother);
Human ded1 = new Human("Alex", 88, true,children1);
Human ded2 = new Human("Roma", 66, true,children2);
Human bab1 = new Human("Lida", 82, false,children1);
Human bab2 = new Human("Lois", 78, false,children2);
Human.add(child1);
Human.add(child2);
Human.add(child3);
Human.add(mother);
Human.add(father);
Human.add(bab1);
Human.add(ded1);
Human.add(bab2);
Human.add(ded2);
System.out.println(Human.toString());
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList<Human> children;
public Human(String name, int age, boolean sex) {
this.name = name;
this.sex = sex;
this.age = age;
}
public Human(String name,int age, boolean sex, ArrayList<Human> children) {
this.name = name;
this.sex = sex;
this.age = age;
this.children = children;
}
public String toString() {
String text = "";
text += "Имя: " + this.name;
text += ", пол: " + (this.sex ? "мужской" : "женский");
text += ", возраст: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", дети: " + this.children.get(0).name;
for (int i = 1; i < childCount; i++) {
Human child = this.children.get(i);
text += ", " + child.name;
}
}
return text;
}
}
}