Что-то даже нет мыслей, где искать ошибку ))
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
ArrayList<Human> children = new ArrayList<>();
children.add(new Human("child1", true, 20, null));
children.add(new Human("child2", false, 18, null));
children.add(new Human("child3", true, 16, null));
ArrayList<Human> father = new ArrayList<>();
father.add(new Human("fath", true, 45, children));
ArrayList<Human> mother = new ArrayList<>();
mother.add(new Human("moth", false, 43, children));
ArrayList<Human> grandparents = new ArrayList<>();
grandparents.add(new Human("gf1", true, 70, father));
grandparents.add(new Human("gm1", false, 68, father));
grandparents.add(new Human("gf2", true, 71, mother));
grandparents.add(new Human("gm2", false, 69, mother));
System.out.println(children.toString());
System.out.println(father.toString());
System.out.println(mother.toString());
System.out.println(grandparents.toString());
}
public static class Human {
private String name;
private boolean sex;
private int age;
private ArrayList<Human> children;
public Human(String name, boolean sex, int age, 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;
}
}
}