ArrayList<String>[] ???

B

baobao

I want to create an array of ArrayLists which contain Strings, like this:

ArrayList<String>[] asdf = new ArrayList<String>[5];


However, whenever I try this I get compile error: generic array creation.

Why is this array generic? It seems everything is quite explicit here. Could
someone please explain to me what is going on here. Thanks in advance!
 
B

Bjorn Abelli

baobao said:
I want to create an array of ArrayLists which contain Strings, like this:

ArrayList<String>[] asdf = new ArrayList<String>[5];


However, whenever I try this I get compile error: generic array creation.

Why is this array generic? It seems everything is quite explicit here.
Could someone please explain to me what is going on here. Thanks in
advance!

The *type* of the array is generic, hence you're trying to create a generic
array...

According to other forums, the answer lies in just that.

Java arrays carry runtime type information that identifies the type of the
elements contained. Generic collections have no runtime type information
about their element type.

Java arrays have the property that there types are covariant, which means
that an array of supertype references is a supertype of an array of subtype
references. In contrast, generic collections are not covariant. An
instantiation of a parameterized type for a supertype is not considered a
supertype of an instantiation of the same parameterized type for a subtype.

But as the collections in Java really are generic by nature, this works
AFAIK:

ArrayList<String>[] asdf = new ArrayList[5];


// Bjorn A
 
F

Fred

"ArrayList<String>[] asdf = new ArrayList[5];" will work. Mixing
generic delcarations and non-generic instantiations, however, is not
recommended by the progressive Java crowd. It'll also produce an
[unchecked] unchecked conversion warning.

ArrayList<String>[] asdf = new ArrayList<String>[5]; is illegal. Look
at the code below to understand why (from 7.3 of Bracha's generics
tutorial http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf):

ArrayList<String>[] asdf = new ArrayList<String>[5]; // illegal
Object o = asdf;
Object[] oa = (Object[]) o;
List<Integer> li = new ArrayList<Integer>();
li.add(new Integer(3));
oa[1] = li; // unsound, but passes run time store check
String s = lsa[1].get(0); // run-time error - ClassCastException

The crux of this example is that failure of the compiler to predict the
exception that occurs on the final line. The use of wildcards
effectively forces the programmer to admit that he is aware of the risk
of such a CCE. Hence why the following is allowed:

List<String>[] asdf = new List<?>[10];

I suggest reading the Generics Tutorial for more explanation (as I'm
not exactly doing an exceptional job of elucidating this myself).

-Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top