In general, no way: arrays in java are covariant by element type, but they must check the type of the assigned value of the element at run time, ie:
Object[] array = new String[10]; // массив строк является массивом объектов array[0] = new Object();
the compiler will skip, but java.lang.ArrayStoreException
will java.lang.ArrayStoreException
on the second line at runtime. Generics in Java are implemented by erasing information about the type parameter, so check that the assigned ArrayList
is of type ArrayList<AAA>
at run time cannot, and the creation of arrays with this type of element is prohibited.
It is usually recommended not to contact, and instead of arrays to use lists. If you really, really want to have an array, you can declare an array of lists without specifying the type:
ArrayList[] a = new ArrayList[] { new ArrayList<AAA>(), new ArrayList<AAA>(), new ArrayList<AAA>()}; a[1] = new ArrayList<AAA>(); AAA value = ((ArrayList<AAA>)a[1]).get( 0 ); AAA otherValue = (AAA)a[1].get( 0 ); // ммм... как в 1.4.2
and lead when used to a parameterized type, but the compiler will give warnings about unchecked cast.
You can also create your own type, which expands the parameterized type with the desired type of parameter.
static class AAAList extends ArrayList<AAA> {}
and use it
AAAList[] a = new AAAList[] { new AAAList(), new AAAList(), new AAAList()}; a[1] = new AAAList(); AAA value = a[1].get( 0 ); ArrayList<AAA> list = a[1];
although this is unlikely to help you.