1

Is it possible to create an array of linked lists? Or an arraylist of linked lists? I've been searching everywhere and seem to be getting contradicting answers. I've seen "no" answers that state that it can't be done because you can't make an array of things that can be dereferenced. I've seen "yes" answers that state it can be done and they end there.

Thanks in advance.

Human Flotsam
  • 33
  • 1
  • 2
  • 6

3 Answers3

5

If I understand you right, you basicly want to make a 2D Array, but with the second half being a linked list.

import java.util.ArrayList;
import java.util.LinkedList;

public class Test
{
    public static void main(String[] args)
    {
        LinkedList one = new LinkedList();
        LinkedList two = new LinkedList();
        LinkedList three = new LinkedList();

        ArrayList<LinkedList> array = new ArrayList<LinkedList>();

        array.add(one);
        array.add(two);
        array.add(three);

        // .. do stuff
    }
}

Java doesn't care what the Objects in Arrays or Lists are, so there's nothing against putting another Array or List in.

Dr. Cyanide
  • 490
  • 1
  • 7
  • 19
  • If set up this way, does each LinkedList get a pointer that has a different name? Or can pointers have the same name for different LinkedLists? (For example, if I want to transverse a linked list, I need to move a pointer along - current = current.next. Could each individual linked list have the same name for the pointers? Or do I need to have different pointer names for each list, eg, currentOne, currentTwo, currentThree, etc.) – Human Flotsam Mar 04 '13 at 03:08
  • I would recommend you should use multiple pointers, if for no other reason than to keep your code cleaner and easier to understand, but you could probably get away with using (current.next).next (or something similar). – Dr. Cyanide Mar 04 '13 at 04:44
4

The worst way: creating an array of raw List:

List[] lstString = new List[10];
for(int i = 0; i < lstString.length; i++) {
    lstString[i] = new LinkedList<String>();
    lstString[i].add(Integer.toString(i));
}
for(int i = 0; i < lstString.length; i++) {
    for(Iterator it = lstString[i].iterator(); it.hasNext(); ) {
        System.out.println(it.next());
    }
}

A slightly better way: use a wrapper class that holds your List and create an array of it:

public class Holder {
    List list = new LinkedList();
}

//...
Holder[] holders = new Holder[10];
for(int i = 0; i < holders; i++) {
    holders[i] = new Holder();
}

Better approach: use a List<List<Data>>:

List<List<Data>> lstOfListOfData = new ArrayList<List<Data>>();
for(int i = 0; i < 10; i++) {
    lstOfListOfData.add(new LinkedList<Data>());
}
Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
0

I've seen "no" answers that state that it can't be done because you can't make an array of things that can be dereferenced

Doesn't matter if the array's member value is null, if you still can "access" that member, and instantiate it, it's not dereferenced. So yes, you can.

Yuri
  • 1