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;
      }
}
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
Arpssss
  • 3,850
  • 6
  • 36
  • 80
  • What error are you getting when you try to create the list? – Chris Dargis Jul 31 '12 at 22:17
  • 4
    What's wrong with `new LinkedList[10]`? – Jeffrey Jul 31 '12 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. – Arpssss Jul 31 '12 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. – Dennis Meng Jul 31 '12 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 – Arpssss Jul 31 '12 at 22:19

1 Answers1

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();
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 2
    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" – Dennis Meng Jul 31 '12 at 22:23