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.

6
  • new LinkedList<??>[666], new ArrayList<LinkedList<??>>(666);. What's your real question? Commented Mar 4, 2013 at 1:40
  • Why would you not be able to? Commented Mar 4, 2013 at 1:41
  • @Perception have you tried to compile that code? Commented Mar 4, 2013 at 1:49
  • @LuiggiMendoza - it shold be fairly obvious that ?? needs to be replaced by an actual type parameter. Commented Mar 4, 2013 at 4:06
  • 2
    @Perception compiler will complain saying generic array creation, next time compile the code before proposing it as solution. Commented Mar 4, 2013 at 4:07

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.)
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).
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>());
}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.