문제

While reading the source code for HashMap class I've noticed that they use (line 149)

transient Entry<K,V>[] table;

And then they initialize it with (line 283) :

table = new Entry[capacity];

And I can't see a warning related to "unchecked or unsafe operations".

But when I've tried to write something simmliar:

TestClass<T>{...
    LinkedList<T>[] arrayOfLists; ....
    arrayOfLists = new LinkedList[capacity]; //warning here

Unfortunately using @SuppressWarnings("unchecked") is not an option for me...

LinkedList<String>[]...

Still the same, so my question is what is the difference I'm missing and what would be a good solution for an array of generic collections which don't cause warnings. By the way @SuppressWarnning is unfortunately not an option for me.

도움이 되었습니까?

해결책

The code in the Java collections api will not compile cleanly. They get warnings as well. You don't see a warning because with the JDK, you already have the .class files (or a .jar) that has the compiled code.

For your own code, just use a list of linked lists:

TestClass<T> { ...
    List<LinkedList<T>> listOfLists; ...
    listOfLists = new ArrayList<LinkedList<T>>(capacity);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top