| 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
|
|
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
|
 |
 |
|
|
subject: Confusion with generics
|
|
|