File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Confusion with generics Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Confusion with generics" Watch "Confusion with generics" New topic
Author

Confusion with generics

Vaibhav Jaiswal
Greenhorn

Joined: Jan 17, 2008
Posts: 12
Why is this not allowed
HashMap<String,String>[] baseMap = new HashMap<String, String>[2];
it says cannot create a generic array
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
Welcome to JavaRanch

It has to do with generics being implemented by erasure.
You would do well to search the Ranch; you find this (by one of our bartenders), and this from the generics FAQ by Angelika Langer which you ought to bookmark.

See whether those links help.
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 757

Arrays are a deprecated type.

Use:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

No they're not. If I need a fixed size "collection" I'll always choose an array instead of an ArrayList. An ArrayList uses an array internally (hence the name), and this array is always at least as big as the size of the List. Most times it's bigger though, so most of the time you're wasting array space.

Now that doesn't matter if you know you will use part of that wasted space later on, but if you know you won't an array is just as easy.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Confusion with generics
 
Similar Threads
Generic for HashMap holding different values
Generic Array Creation
Adding different objects to same in HashMap?
Help with Java Generics...
same key for diff values in HashMap