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

array of collection

 
Prahlad Joshi
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20834
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jan Nowak
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic