Компилятор IDE выдаёт Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
по мне дак звучит как ArrayList не может быть приведён к ArrayList, что странно
собственно явное приведение использовал только потому что до этого компилятор ругался что он не знает тип Листа <>, поэтому указал ему (ArrayList<Human>), так ему опять не нравится и главное не понятно, почему?
п.с. понимаю, что можно легко решить сформировав параметр конструктора (список) до этого конструктора, но просто интересно почему так как у меня не работает?
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
Human reb1 = new Human("sdfsd", true,12,null);
Human reb2 = new Human("sdfsd", true,12,null);
Human reb3 = new Human("sdfsd", true,12,null);
Human pap = new Human("sdf",false,45, (ArrayList<Human>) Arrays.asList(reb1,reb2,reb3));
Human mam = new Human("sdf",true,45, (ArrayList<Human>) Arrays.asList(reb1,reb2,reb3));
Human dad1 = new Human("sdf",true,75, (ArrayList<Human>) Arrays.asList(pap));
Human dad2 = new Human("sdf",true,75, (ArrayList<Human>) Arrays.asList(mam));
Human gmam1 = new Human("sdf",false,75, (ArrayList<Human>) Arrays.asList(pap));
Human gmam2 = new Human("sdf",false,75, (ArrayList<Human>) Arrays.asList(mam));
System.out.println(reb1);
System.out.println(reb2);
System.out.println(reb3);
System.out.println(pap);
System.out.println(mam);
System.out.println(dad1);
System.out.println(dad2);
System.out.println(gmam1);
System.out.println(gmam2);
}
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;
}
}
}