Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly define an array of linked list in Java ? [duplicate]

I tried to define an array of linked list in Java like the following, 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 would not even compile and generate this error instead.

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

So, how should I define my array of linked list properly ?

like image 989
mynameisJEFF Avatar asked Dec 26 '14 07:12

mynameisJEFF


People also ask

How do you create a duplicate linked list in Java?

LinkedList clone() Method in Java clone() method is used to create a shallow copy of the mentioned linked list. It just creates a copy of the list. Parameters: This method does not take any parameters. Return Value: This function returns a copy of the instance of Linked list.

Does linked list allow duplicates in Java?

4) ArrayList and LinkedList also allow duplicates and null, unlike any other List implementation e.g. Vector.

Can you have an array of linked lists in Java?

A linked list is a sequence of data structures, which are connected together via links. To create an array of linked lists, create required linked lists and, create an array of objects with them.


1 Answers

This is a proper 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 different types are inserted into an array:

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

If you try the same thing with a generic list, there would be a problem:

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

If arrays of parameterized lists were allowed, the previous code would fail to throw the desired ArrayStoreException.

Taken from docs.oracle.com

So what can I store in hashtable[] ?

Does it mean I am now allowed to have a linked list of string in the hashtable[0] and a linked list of Long in hashtable1, if I do LinkedList [] hashtable = new LinkedList[10]?

No, compiler won't allow you to store LinkedList to the hashtable array directly. Following snippet won't compile:

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

However you can store the LinkedList without type parameters, or even a subclass of 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();

You can store the LinkedList if you cast your array to LinkedList[]. However you won't be able to store the anything else but a LinkedList:

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

Object[] objectHashTable = rawHashTable;
objectHashTable[5] = "This line will throw an ArrayStoreException ";
like image 83
bedrin Avatar answered Sep 27 '22 21:09

bedrin