aspose file tools*
The moose likes Java in General and the fly likes generics and arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "generics and arrays" Watch "generics and arrays" New topic
Author

generics and arrays

Jim Lang
Greenhorn

Joined: Oct 04, 2000
Posts: 27
Dear ranchers,

This is a bit of a painful one, since I know that Java 5 does not permit the construction of generic arrays. It seems strange that this is not permitted since to implement collection classes using arrays would be a natural thing to do.

Fair enough, I see it's not a proper type to create an array with, and I can see that generics are only doing their thing at compile-time (so this might cause problems for runtime type-checking) but shouldn't there be some sort of way to do this using 'nice' java?

Can anyone suggest a way to do this that isn't a nasty hack (or an only mildly nasty hack if there's no nice way to do it)?

Many thanks - Jim
Jeff Mayer
Greenhorn

Joined: Mar 21, 2007
Posts: 9
Maybe something like that:

?
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
The simplest way is to not use arrays. Arrays are not fully typechecked at compile time:

Number[] numbers=new Integer[1];
numbers[0]=5.0; //ArrayStoreException

That's the main reason why they're incompatible with generics.

If you're absolutely convinced that using arrays is what you want, use the factory pattern.

interface ArrayFactory<T>
{
T[] instance();
}

public <T> void doSomething(ArrayFactory<T> factory)
{
System.out.println(factory.instance());
}

Obviously this isn't a useful example.

In my opinion, Java 5 wasn't fit for release while there were generics warnings from classes such as ArrayList.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

"This is a bit of a painful one, since I know that Java 5 does not permit the construction of generic arrays. It seems strange that this is not permitted since to implement collection classes using arrays would be a natural thing to do."

It is strange indeed, I've written a blog post once to explain exactly why it works like this in Java: Java generics quirks.

Basically the reason why this doesn't work is because the Java compiler uses type erasure, so that the array store check does not have all the information available at runtime that it needs. I think Sun implemented generics using type erasure only because they didn't want to radically change the class file format in Java 5. Unfortunately we're now stuck with it - it seems unlikely that in future versions of Java we will get rid of type erasure.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
Reification is being considered for Java 7.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics and arrays