I've found out that if you have a generic class with an array that depends of type parameter of that class, you can't initialize that array in a usual way:
class Foo<T> {
private T[] a;
Foo() {
a=new T[5]; //doesn't compile
}
}
You can only do something like this:
class Foo<T> {
private T[] a;
Foo(T[] a) {
this.a=a;
}
}
But this code is possible:
ArrayList<Integer> list=new ArrayList<>();
Why? How does the ArrayList
overcome that thing? I've looked into its code but couldn't figure it out. It seems like it just stores things in Object[]
array which seems wrong.