• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java 5 - Array of ArrayList<String> ?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>> !
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic