5

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 ?

mynameisJEFF
  • 4,073
  • 9
  • 50
  • 96

3 Answers3

9

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 ";
bedrin
  • 4,458
  • 32
  • 53
  • This is what I did in the first place but it generated 2 warnings , which makes me feel really unsafe about this approach. – mynameisJEFF Dec 26 '14 at 07:47
  • @mynameisJEFF I have added some more details why it's a proper way. You can use _@SuppressWarnings("unchecked")_ to suppress this warning – bedrin Dec 26 '14 at 07:49
  • So, 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 `hashtable[1]`, if I do `LinkedList [] hashtable = new LinkedList[10]`? – mynameisJEFF Dec 26 '14 at 07:52
  • @mynameisJEFF I have updated my answer with a section on what can we store in _hashtable_ – bedrin Dec 26 '14 at 08:07
  • I think I kind of understand what you are now. So basically, if I defined ` LinkedList[] hashtable = new LinkedList[10];`, I have to initialize a new linked list (and it must be of type `Long`) at each `hashtable[i]` entry in order to store linked list of Long data into the array. Am I correct? – mynameisJEFF Dec 26 '14 at 08:20
  • @mynameisJEFF exactly! – bedrin Dec 26 '14 at 08:27
6

First of all define the array size where each element is a LinkedList.

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

Now since each element in the array is a LinkedList itself and all of them are null each of them needs to be initialized. Hence,

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

If you want to add data to a list, then do it like this:

hashTable[i].add(YOUR_LONG_DATA_HERE);

and finally to iterate,

for (int i=0;i<10;i++){
        for (Long j: hashTable[i])
            System.out.println(j);
}
Saif
  • 6,804
  • 8
  • 40
  • 61
Tahmid Ali
  • 805
  • 9
  • 29
0

If you need a list/array of LinkedList, you could use an ArrayList to hold the collection with an initial size of 10.

Here is an alternative approach you could try:

ArrayList<LinkedList<Long>> list = new  ArrayList<LinkedList<Long>>(10);
ZakiMak
  • 2,072
  • 2
  • 17
  • 26