user50210
user50210

Reputation: 113

Raw Types in Java

I am working through a text book which has the following code:

Entry<K,V>[] tempTable = (Entry<K,V>[]) new Entry[size];

When I compile with -Xlint, it says the Entry on the right is missing the type arguments. However, I cannot add the type arguments, as this would lead to generic array creation. I guess my question is two-part:

  1. I assume that the code in the text compiles correctly, so is there something I could do to make this work as is?
  2. What is the most elegant way to do this?

Upvotes: 1

Views: 95

Answers (2)

newacct
newacct

Reputation: 122429

Entry<K,V>[] tempTable = (Entry<K,V>[]) new Entry<?,?>[size];

Upvotes: 1

Mohammad Najar
Mohammad Najar

Reputation: 2009

Your best bet would be to use the Collection interface, List to

1- make it more elegant, and

2- get rid of the warnings

List<Entry<K, V>> tempTable = new  ArrayList<>();       
tempTable.add(new Entry<Integer, Double>());  // or whatever object.

I hope this helps.

Upvotes: 1

Related Questions