Я посмотрел уже даже в решённых вопросах что у меня +- всё в порядке , почему компилятор вообще ничего не принимает ? Что за бред...
Абсолютно такая же ситуация если оставить в место :
ArrayList<Human> children = new ArrayList<>(); просто :ArrayList<Human> children; Если запускаю у себя , то выводиться вот что : Имя: Tom, пол: мужской, возраст: 49, дети: Max
Имя: Tom2, пол: мужской, возраст: 52, дети: Maria
Имя: Melisa, пол: женский, возраст: 45, дети: Max
Имя: Melisa2, пол: женский, возраст: 47, дети: Maria
Имя: Max, пол: мужской, возраст: 22, дети: Sarah, Martin, Zac
Имя: Maria, пол: женский, возраст: 21, дети: Sarah, Martin, Zac
Имя: Sarah, пол: женский, возраст: 3
Имя: Martin, пол: мужской, возраст: 2
Имя: Zac, пол: мужской, возраст: 1package com.javaboorush.task.task08.task0824;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
ArrayList<Human> children = new ArrayList<>();
ArrayList<Human> childrenDad = new ArrayList<>();
ArrayList<Human> childrenMom = new ArrayList<>();
Human child1 = new Human("Sarah",false,3);
Human child2 = new Human("Martin",true,2);
Human child3 = new Human("Zac",true,1);
children.add(child1);
children.add(child2);
children.add(child3);
Human dad = new Human("Max",true,22,children);
Human mom = new Human("Maria",false,21,children);
childrenDad.add(dad);
childrenMom.add(mom);
Human grandF = new Human("Tom",true,49,childrenDad);
Human grandF2 = new Human("Tom2",true,52,childrenMom);
Human grandM = new Human("Melisa",false,45,childrenDad);
Human grandM2 = new Human("Melisa2",false,47,childrenMom);
System.out.println(grandF);
System.out.println(grandF2);
System.out.println(grandM);
System.out.println(grandM2);
System.out.println(dad);
System.out.println(mom);
System.out.println(child1);
System.out.println(child2);
System.out.println(child3);
}
public static class Human {
//напишите тут ваш код
String name;
boolean sex;
int age;
ArrayList<Human> children = new ArrayList<>();
Human(String name , boolean sex,int age){
this.name = name;
this.sex = sex;
this.age = age;
}
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;
}
}
}