How to correctly define an array of linked list in Java?

I tried to define an array of linked list in Java, as shown below, which compiled fine, but it generated 2 warning messages.

 LinkedList<Long> [] hashtable = new LinkedList[10];

warning: [rawtypes] found raw type: LinkedList
    LinkedList<Long> [] hashtable = new LinkedList[10];
                                        ^
  missing type arguments for generic class LinkedList<E>
  where E is a type-variable:
    E extends Object declared in class LinkedList
HashTable.java:13: warning: [unchecked] unchecked conversion
    LinkedList<Long> [] hashtable = new LinkedList[10];
                                    ^
  required: LinkedList<Long>[]
  found:    LinkedList[]

So i tried

 LinkedList<Long> [] hashtable = new LinkedList<Long>[10];

But this time it does not even compile and does not generate this error.

HashTable.java:13: error: generic array creation
    LinkedList<Long> [] hashtable = new LinkedList<Long>[10];
                                    ^
1 error

So, how should I correctly define my array of linked lists?

+4
source share
3 answers

This is the correct way to create an array:

@SuppressWarnings("unchecked") LinkedList<Long> [] hashtable = new LinkedList[10];

Cannot create arrays of parameterized types

You cannot create arrays of parameterized types. For example, the following code does not compile:

List<Integer>[] arrayOfLists = new List<Integer>[2];  // compile-time error

The following code illustrates what happens when various types are inserted into an array:

Object[] strings = new String[2];
strings[0] = "hi";   // OK
strings[1] = 100;    // An ArrayStoreException is thrown.

, :

Object[] stringLists = new List<String>[];  // compiler error, but pretend it allowed
stringLists[0] = new ArrayList<String>();   // OK
stringLists[1] = new ArrayList<Integer>();  // An ArrayStoreException should be thrown,
                                            // but the runtime can't detect it.

, ArrayStoreException.

docs.oracle.com

, hashtable []?

, hashtable [0] Long in hashtable 1, LinkedList [] hashtable = new LinkedList [10]?

, LinkedList -. :

hashtable[0] = new LinkedList<String>();

LinkedList LinkedList:

@SuppressWarnings("unchecked") LinkedList<Long>[] hashtable = new LinkedList[10];

hashtable[0] = new LinkedList<Long>();
hashtable[1] = new MyLinkedList<Long>();
hashtable[2] = new LinkedList();
hashtable[3] = new MyLinkedList();

LinkedList, LinkedList []. , LinkedList:

LinkedList[] rawHashTable = hashtable;
rawHashTable[4] = new LinkedList<String>();

Object[] objectHashTable = rawHashTable;
objectHashTable[5] = "This line will throw an ArrayStoreException ";
+6

, LinkedList.

LinkedList<Long> hashTable[] = new LinkedList[10];

, LinkedList, null, . ,

for (int i=0;i<10;i++)
        hashTable[i] = new LinkedList<Long>();

, :

hashTable[i].add(YOUR_LONG_DATA_HERE);

, , ,

for (int i=0;i<10;i++){
        for (Long j: hashTable[i])
            System.out.println(j);
}
+4

/ LinkedList, ArrayList 10.

, :

ArrayList<LinkedList<Long>> list = new  ArrayList<LinkedList<Long>>(10);
0

Source: https://habr.com/ru/post/1569022/


All Articles