• 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

array of collection

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList<String>[] coupledTo;

coupledTo=new ArrayList<String>[5];

//the above line is generating compiler error(generic array creation) while //the below one is not
coupledTo=new ArrayList[5];

i can't understand why
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe this is a known issue for Java 5. Do anyone know if this has been fixed for Java 6 or higher.

Henry
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know this is perfectly legal.
From Angelica Langer FAQ on generics:

You can declare a reference variable of an array type whose component type is a concrete parameterized type. Arrays of such a type must not be created. Hence, this reference variable cannot refer to an array of its type. All that it can refer to is null , an array whose component type is a non-parameterized subtype of the concrete parameterized type, or an array whose component type is the corresponding raw type. Neither of these cases is overly useful, yet they are permitted.



So these are allowed:


[ November 25, 2007: Message edited by: Jan Nowak ]
 
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

Originally posted by Henry Wong:
I believe this is a known issue for Java 5. Do anyone know if this has been fixed for Java 6 or higher.


It's not a bug in Java 5 - it's a consequence of how generics and arrays are implemented in Java. See my blog entry for more details.

This might be solved when generics are implemented differently (without type erasure). People are thinking about how to do this in Java 7.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
This might be solved when generics are implemented differently (without type erasure). People are thinking about how to do this in Java 7.



Hallelujah! Is there a JSR for this? If not, do you have a link to where I can read more about this ongoing design work?

Personally, I think Java's generics are one of its few truly botched language features. I mean, it's amazing that they managed to accomplish so much with the type erasure mechanism, but in my experience this seems to confuse so many developers that I really wonder whether the added type safety was worth it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic