0

I'm try to create a HashTable and this part of the code requires an array, but when it's not delcared generic I get unchecked warnings, but I know that generic arrays aren't supported, but I'm not sure how to fix this.

array = new HashEntry<AnyType>[ nextPrime( arraySize ) ];
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436

3 Answers3

1

I suggest checking out JDK's own code for HashMap, specifically the resize method and these lines:

    @SuppressWarnings({"rawtypes","unchecked"})
        Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];

newTab is then assigned to the main instance variable, table. So, if JDK can't avoid @SuppressWarnings, neither will you.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Write it as such

@SuppressWarnings("unchecked")
HashEntry<K, V>[] array = new HashEntry[nextPrime()];

then add @SuppressWarnings("unchecked") to it.

MJSG
  • 1,035
  • 8
  • 12
0

Don't use arrays. Use collections.

Using arrays requires more code, more care and gives no measurable benefit.

Using collections leverages the code and care built into the JDK.
If you've got an array of HashEntry, you're most of the way to a Map - just use a constant order map:

Map<SomeKey, SomeValue> map = new LinkedHashMap<SomeKey, SomeValue>();
Bohemian
  • 412,405
  • 93
  • 575
  • 722