aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array of collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array of collection" Watch "array of collection" New topic
Author

array of collection

Prahlad Joshi
Ranch Hand

Joined: Apr 21, 2007
Posts: 44
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
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

I believe this is a known issue for Java 5. Do anyone know if this has been fixed for Java 6 or higher.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jan Nowak
Greenhorn

Joined: Aug 12, 2007
Posts: 16
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

Joined: Aug 16, 2005
Posts: 14352
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
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.


SCJP 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array of collection