I am a bit puzzled: I want an array of ArrayList-s but I see no way to specify the ArrayList item type:
class Test<V> {
final static int N = 512;
ArrayList<V> list1 = new ArrayList<>(); // ok
ArrayList<V> list2 = new ArrayList<V>(); // ok
ArrayList<V>[] lists1 = new ArrayList[N]; // <== warning:
// Type safety: The expression of type ArrayList[] needs
// unchecked conversion to conform to ArrayList<V>[]
// Note that this line works because it's only a warning.
ArrayList<V>[] lists2 = new ArrayList<V>[N]; // <== error:
// Cannot create a generic array of ArrayList<V>
ArrayList<V>[] lists3 = new ArrayList<>[N]; // <== error:
// Incorrect number of arguments for type ArrayList<E>;
// it cannot be parameterized with arguments <>
ArrayList<V>[] lists4 = (ArrayList<V>[])new ArrayList[N]; // <== warning:
//Type safety: Unchecked cast from ArrayList[] to ArrayList<V>[]
}
I see that the problem with
ArrayList<V>[] lists1 = new ArrayList[N];
is that the type of items is not specified, but how do I specify it?
EDIT: the answer is that they disallowed raw arrays of parameterized types. They probably wanted the programmer to explicitly take responsibility for type safety. Or maybe they felt uneasy because Test.f() compiles without any warnings:
void f() {
lists1[0] = new ArrayList<V>(); // ok
//lists1[1] = new ArrayList<String>(); // <== error:
// Type mismatch: cannot convert from ArrayList<String> to ArrayList<V>
Object[] x = lists1;
x[0] = new ArrayList<Integer>(); // ok
}
They probably decided that a warning in some barely relevant place is better than no warning at all.