title: Initializing Singly Linked List Java
body: <p>I am trying to implement an abstract data type of NumList in class NumLinkedList as a singly linked list. </p>
  
  <pre><code>public class NumLinkedList implements NumList
  {
  
  Node head;
  int nItem;
  
  private class Node
  {
      public Node next;
      public double value;
      public Node(double i, Node j)
      {
          value = i;
          next = j;
  
      }
  
  }
  
  public NumLinkedList()
  {
      head = null;
      nItem = 0;
  
  }
  </code></pre>
  
  <p>is my initialization and I am having trouble with the following methods.</p>
  
  <pre><code>public void print()
  {
      Node currNode = head;
      while(currNode.next != null)
      {
          currNode = currNode.next;
          System.out.println(currNode.value);
      }
  
  }
  
  public int size()
  {
      int size = 0;
      Node currNode = head;
      while(currNode.next != null)
      {
          currNode = currNode.next;
          size++;
          nItem++;
  
      }
      return size;
  
  }
  
  public void insert(int i, double value)
  {
      if( i &lt; 0)
      {
          System.out.println("Error. out of bounds.");
      }
      if ( i &gt; size())
      {
          Node currNode = head;
          for(int j = 0; j &lt; i; j++)
          {
              currNode = currNode.next;
          }
          currNode.next = new Node(value,null);
      }
  
      if ( i &lt;= size())
      {
          Node currNode = head;
          for(int j = 0; j &lt; i-1; j++) // this moves CurrNode to the i-1th node
          {
              currNode = currNode.next;
          }
          currNode.next = new Node(value,currNode.next);
      }
  
      nItem++;
  }
  </code></pre>
  
  <p>when I run my testing code, </p>
  
  <pre><code>public static void main (String[] args)
  {
      NumLinkedList test;
      test = new NumLinkedList();
  
      //System.out.println("this is how many initial items the initialized list has");
      //System.out.println(test.size());
      test.insert(1, 0.1);
      System.out.println("have tried to insert value 0.1 @ position 1, that is the first element in list.");
      test.print();
      System.out.println("tried print(). did it work?");
      test.insert(4, -1);
  </code></pre>
  
  <p>I get an error in the </p>
  
  <pre><code>test.insert(1, 0.1);
  </code></pre>
  
  <p>, referring to </p>
  
  <pre><code>if ( i &gt; size())
  </code></pre>
  
  <p>and </p>
  
  <pre><code>while(currNode.next != null)
  </code></pre>
  
  <p>as I failed initializing my array ADT as well, I believe that my list ADT is also mis-initialized. It's hard to find proper examples on Google, any references for ADT initialization?</p>