0

I have created my own custom Linked List (code is below). Now, I can't understand how to create an array of that Linked list like LinkedList[] l = new LinkedList[10]. Can anyone help me.

class Node {
      public int data;
      public Node pointer;
}

class LinkedList {
      Node first;
      int count = 0;

      public void addToEnd(int data){
            if(first == null){
                  Node node = new Node();
                  node.data = data;
                  node.pointer = null;
                  first = node;
                  count = 1;
                  return;
            }
            Node next = first;
            while(next.pointer != null){
                  next = (Node)next.pointer;
            }
            Node newNode = new Node();
            newNode.data = data;
            newNode.pointer = null;
            next.pointer = newNode;
            count++;
      }

      public Node getFirst(){
            return first;
      }
      public Node getLast(){
            Node next = first;
            while(next.pointer != null)
                  next = next.pointer;
            return next;
      }


      public int[] get(){
        if(count != 0){
            int arr[] = new int [count] ;
            Node next = first;
            int i = 0;
                  arr[0]= next.data;
            while(next.pointer != null){
                  next = next.pointer;
                  i++;
                  arr[i] = next.data;
            }
            i++;
            return arr ;
            }
            return null ;
      }
      public int count(){
            return count;
      }
}
5
  • What error are you getting when you try to create the list? Commented Jul 31, 2012 at 22:17
  • 4
    What's wrong with new LinkedList[10]? Commented Jul 31, 2012 at 22:17
  • @DougRamsey, LinkedList[] l = new LinkedList[2]; for(int i=0 ; i<10 ; i++) l[0].addToEnd(i); int arr[] = l[0].get(); System.out.println(arr[0]); gives error. Commented Jul 31, 2012 at 22:18
  • 1
    Probably NullPointerException. I'll write an answer explaining why. Or rather, I would have if someone didn't do it first. Commented Jul 31, 2012 at 22:19
  • @Jeffrey, LinkedList[] l = new LinkedList[2]; for(int i=0 ; i<10 ; i++) l[0].addToEnd(i); int arr[] = l[0].get(); System.out.println(arr[0]); gives error. But, only for l (not array) gives OK Commented Jul 31, 2012 at 22:19

1 Answer 1

4

I'm going to guess that your problem is just that when you create an array of objects, like

LinkedList[] lists = new LinkedList[10];

you get an array full of nulls; you need to create objects to store in the array:

for (int i=0; i<lists.length; ++i)
    lists[i] = new LinkedList();
Sign up to request clarification or add additional context in comments.

1 Comment

For Object arrays, the way I see it is that declaring the array is like taking a bunch of boxes and saying "Hey, I'm going to store <*some object*> into these. I haven't actually put anything in them yet, but when I do, they'll be <*some object*>s"

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.