Why is the output this:
[Name: jose, sex: male, age: 55, children: jose, maria, Name: maria, sex: female, age: 56, children: jose, maria]
[Name: mario, sex: male, age: 57, children: jose, maria, Name: ze, sex: female, age: 54, children: jose, maria]
[Name: gaspar, sex: male, age: 35, children: mario, ze, Name: constanca, sex: female, age: 34, children: mario, ze]
[Name: Thomas, sex: male, age: 2, children: gaspar, constanca, Name: gabriel, sex: male, age: 2, children: gaspar, constanca, Name: jose, sex: male, age: 5, children: gaspar, constanca]
When i have separate them in groups?
package com.codegym.task.task08.task0824;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public static void main(String[] args) {
//write your code here
ArrayList<Human> one = new ArrayList<>();
ArrayList<Human> two = new ArrayList<>();
ArrayList<Human> three = new ArrayList<>();
ArrayList<Human> four = new ArrayList<>();
Human grandfather1 = new Human("jose", true, 55, one);
Human grandmother1 = new Human("maria", false, 56, one);
Human father = new Human("gaspar", true,35, two);
Human grandfather2 = new Human("mario", true, 57, one);
Human grandmother2 = new Human("ze", false, 54, one);
Human mother = new Human("constanca", false, 34, two);
Human child1 = new Human("Thomas", true, 2, three);
Human child2 = new Human("gabriel", true, 2, three);
Human child3 = new Human("jose", true, 5, three);
Collections.addAll(one,grandfather1, grandmother1);
Collections.addAll(two,grandfather2, grandmother2);
Collections.addAll(three,father,mother);
Collections.addAll(four,child1, child2, child3);
System.out.println(one.toString());
System.out.println(two.toString());
System.out.println(three.toString());
System.out.println(four.toString());
}
public static class Human {
//write your code here
ArrayList<Human> children = new ArrayList<>();
String name;
boolean sex;
int age;
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 += "Name: " + this.name;
text += ", sex: " + (this.sex ? "male" : "female");
text += ", age: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", children: " + this.children.get(0).name;
for (int i = 1; i < childCount; i++) {
Human child = this.children.get(i);
text += ", " + child.name;
}
}
return text;
}
}
}