Forums Register Login

Java 5 - Array of ArrayList<String> ?

+Pie Number of slices to send: Send
After two hours of frustration, thought I'd post.
I'm trying to upgrade my application to Java 5 (JDK 1.5.0).
Generics frustrate me. Printed out the transition PDF
white paper. Read through it and marked it up. Still
having difficulty.

Simply stated, how does one declare and initialize an
array of three ArrayLists whose generic type (for example)
is String ? Is the secret in the usage of a wildcard "?" ?

Here's how it was done with 1.4.2 ... simple ...

ArrayList [] array = new ArrayList []
{ new ArrayList(), new ArrayList(), new ArrayList() };

or ArrayList [] array = new ArrayList [3]; // 3 null elements

I tried all sorts of variations, even tried using
the Arrays newInstance() method, but ... still getting
compilation warnings. I'm obviously missing sumpthin' !

Hey - Thanks !

- Eric
+Pie Number of slices to send: Send
You can't.

Because of the semantics of Java array classes, such an array can't be made type safe. Generics were designed so that if you get no warnings, you're guaranteed not to get ClassCastExceptions at runtime (for any code without casts, anyway.) But Java arrays are unfortunately designed so that if A is a subclass of B, then A[] is a subclass of B[]. That means that if arrays of generics were legal, you could assign your ArrayList<String>[] to an Object[] without a cast, store an ArrayList<Integer> into that array (remember that because of erasure, the real underlying type would just be ArrayList[], so this would be fine) and then later use of your ArrayList<String> would end up throwing a ClassCastException when somebody found an Integer where they expected a String.

So... what do you do? You just use ArrayList[]... or you use ArrayList<ArrayList<String>> !
+Pie Number of slices to send: Send
One of the quirks with generics in Java is that you can't create arays of generic types.

The reason for this is because a feature of Java arrays (covariance) and the way generics are implemented in Java (via type erasure) are unfortunately not compatible with each other...

I wrote a blog entry about it a while ago with a more detailed explanation.
[ July 31, 2006: Message edited by: Jesper Young ]
+Pie Number of slices to send: Send
Thanks to both of you for your thoughtful replies.

I don't like the answer but I guess I'll just ignore the
grouchy compilation warnings.
I knew I would regret that burrito. But this tiny ad has never caused regrets:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2417 times.
Similar Threads
Need help on two-dimensional array
Printing Treemap, value and key in pop-up window
Can an arraylist hold null reference or not using add(Index i, Object obj) method?
outputing an arraylist of arraylists to a file.
How to delete an index.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:36:31.