Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 5 - Array of ArrayList<String> ?  RSS feed

 
Eric Blische
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>> !
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Eric Blische
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!