
Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
> Create the array slots in one step, then initialise the slots in a
> statement each.
Thanks for your reply. I am still not sure I get it.
Hashtable<String, NodeAddress>[] cache = new Hashtable;
cache[0] = new Hashtable<String, NodeAddress>();
cache[1] = new Hashtable<String, NodeAddress>();
cache[2] = new Hashtable<String, NodeAddress>();
This compiles with warnings since the type of the type of the
Hashtable array is not set correctly, "Uses unsafe operations...".
My experience with the type stuff in Java is a bit rusty. Sorry.
Replacing in the above code:
Hashtable<String, NodeAddress>[] cache = new Hashtable;
with:
Hashtable<String, NodeAddress>[] cache = new Hashtable<String,
NodeAddress>[3];
blows up with "generic array creation"
How can I do it properly?
Jon.
Roedy Green - 04 Jul 2008 03:10 GMT
>How can I do it properly?
Java is simply buggered when it comes to mixing arrays and generics.
Even inside Sun's collection classes they have to cheat with code that
generates warnings.
I ran into a similar problem with this code:
Map.Entry<String, Integer>[] keyValuePairs =
justEntries.toArray( new
Map.Entry[justEntries.size()] );
// Type erasure won't let us say:
// Map.Entry<String, Integer>[] keyValuePairs =
// justEntries.toArray ( new
// Map.Entry<String,Integer>[justEntries.size()] );
Just close your eyes and gag it down.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lew - 04 Jul 2008 04:02 GMT
> Hashtable<String, NodeAddress>[] cache = new Hashtable;
> ...
> This compiles with warnings since the type of the type of the
> Hashtable array is not set correctly, "Uses unsafe operations...".
I doubt it. The expression 'new Hashtable;' should have choked with an error,
not a warning.
Once you fix that, you'll get the warnings you describe. Those are normal and
acdepted whenever you succumb to the temptation to use raw types. You'd be
better off avoiding raw types and sticking with generics.

Signature
Lew
Roedy Green - 04 Jul 2008 05:45 GMT
>Hashtable<String, NodeAddress>[] cache = new Hashtable;
other than the problems mixing Generics and arrays you are missing
[3].

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
>> I tried the following, which does not compile:
>>
>> private Hashtable<String, NodeAddress>[] cache = {new
>> Hashtable<String, NodeAddress>(),new Hashtable<String,
>> NodeAddress>()};
How about actually *citing* the error message?
> ~/projects/testit/src/testit/Foo.java:22: generic array creation
I guess that means you can't create a generic array.
Could that be? Could the error message actually have already answered your
question?
Let's research:
<http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.3>
> It is a compile-time error if the element type is not a reifiable type (§4.7)
<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.7>
> A type is reifiable if and only if one of the following holds:
>
[quoted text clipped - 4 lines]
> * It is a primitive type (§4.2).
> * It is an array type (§10.1) whose component type is reifiable.
So the answer is, no, you cannot do that.
Anyway, arrays and generics don't play well together. Make a List of Maps
instead.
Additionally, I suggest using HashMap instead of Hashtable, and declaring the
variable in terms of List <Map> rather than a concrete type.
private List <Map <String, Foo>> cache
= new ArrayList <Map <String, Foo>> ();
{
cache.add( new HashMap <String, Foo> ());
cache.add( new HashMap <String, Foo> ());
}

Signature
Lew
Roedy Green - 04 Jul 2008 05:46 GMT
>How about actually *citing* the error message?
http://mindprod.com/jgloss/compileerrormessages.html#GENERICARRAY

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Lew - 04 Jul 2008 05:53 GMT
Lew wrote:
>> How about actually *citing* the error message?
> http://mindprod.com/jgloss/compileerrormessages.html#GENERICARRAY
Yep.
My point to the OP was that it's useful actually to *cite* the error message,
not just say, "... does not compile". The second point to the OP was that the
error message *actually describes the problem*, albeit tersely. By citing the
error message, I was able to show how it led to research in the JLS, which in
turn led to the complete answer.
So, OP and others who want to discuss an error message on these forums: How
about actually *citing* the error message?

Signature
Lew
Roedy Green - 04 Jul 2008 06:36 GMT
>So, OP and others who want to discuss an error message on these forums: How
>about actually *citing* the error message?
One further advantage is if you have the precise wording of the error,
you can look it up at
http://mindprod.com/jgloss/compileerrormessages.html
or
http://mindprod.com/jgloss/runerrormessages.html
By browsing the page and using your browser find feature.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
jonbbbb - 04 Jul 2008 10:16 GMT
> >> I tried the following, which does not compile:
>
[quoted text clipped - 41 lines]
> cache.add( new HashMap <String, Foo> ());
> }
Thank you very much for the information.
I will try your code.
Sorry for not being more explicit in citing the error message.
Jon.
Roedy Green - 04 Jul 2008 20:04 GMT
>Anyway, arrays and generics don't play well together. Make a List of Maps
>instead.
At least his sweeps the kludge under the rug inside ArrayList. This
creates the cleanest code. In theory, you could use the same
bailingwire that ArrayList does.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com