title: Java LinkedList adding multiple nodes
body: <p>my question is in my main method, how to add multiple nodes to the linked list....What I have now with first, node2, node3..I thought was adding those nodes but I realized I don't think I'm actually doing anything with those nodes and their values, right?  How do I use setData() and setNext() to add all of those nodes.  Does that make sense?  </p>
  
  <pre><code>ListNode&lt;String&gt; node4 = new ListNode&lt;String&gt;("Fourth", null);
  ListNode&lt;String&gt; node3 = new ListNode&lt;String&gt;("Third", node4);
  ListNode&lt;String&gt; node2 = new ListNode&lt;String&gt;("Second", node3);
  ListNode&lt;String&gt; first = new ListNode&lt;String&gt;("First", node2);
  </code></pre>
  
  <p>If the above sets up the values how do I add them all?</p>
  
  <p>Do I then need to set the data and next for each one of these?  (This seems redundant since I seem to be setting up the value of each nodes data and next in the constructor above?)</p>
  
  <pre><code>first.setData("first");
  first.setNext(node2); 
  node2.setData("Second");
  node2.setNext(node2);
  //.....
  </code></pre>
  
  <p>I'm trying to add all of the above nodes so I can test my addLast() method by adding a new node.  However, when I call my addLast() method in main as you can see below the only thing that's printed is that addLast() value I added (and first if I call addFirst()).  </p>
  
  <p>Test Class</p>
  
  <pre><code>public class LinkedListDriver
  {
  
      public static void main(String[] args) {
          //List&lt;String&gt; list = new LinkedList&lt;String&gt;();                   //comment out this line to test your code
          SinglyLinkedList&lt;String&gt; list = new SinglyLinkedList&lt;String&gt;(); //remove comment to test your code
  
          ListNode&lt;String&gt; node4 = new ListNode&lt;String&gt;("Fourth", null);
          ListNode&lt;String&gt; node3 = new ListNode&lt;String&gt;("Third", node4);
          ListNode&lt;String&gt; node2 = new ListNode&lt;String&gt;("Second", node3);
          ListNode&lt;String&gt; first = new ListNode&lt;String&gt;("First", node2);
  
          ListNode value = new ListNode("First", new ListNode("Second", new ListNode("Third", null)));
  
          //I've been messing around with this but 
          list.addFirst(first.getData());
          list.addFirst("Second");
  
          list.addLast("Fifth");
          list.printList();
      }
  }
  </code></pre>
  
  <p>I didn't add my other two classes because I didn't think it was relevant but if you'd like to see it let me know.  I'm very new this is only my second class and it's online and is poorly constructed class, please be nice lol</p>
  
  <p>SinglyLinkedList class</p>
  
  <pre><code>//This class implements a very simple singly-linked list of Objects
  public class SinglyLinkedList&lt;E&gt;
  {
      ListNode&lt;E&gt; first; // first element
  
      public SinglyLinkedList() {
          first = null;
      }
  
      public E getFirst() {
          if (first == null) {
              throw new NoSuchElementException();
          } else
              return first.getData();
      }
  
      public void addFirst(E value) {
          first = new ListNode&lt;E&gt;(value, first);
      }
  
      // Methods below implemented by you. Note: while writing methods, keep in mind
      // that you might be able to call other methods in this class to help you - you
      // don't always need to start from scratch(but you'll have to recognize when)
  
      public void addLast(E value) {
          ListNode&lt;E&gt; temp = first;
          //If list is empty make new node the first node.
          if (temp == null) {
              first = new ListNode &lt;E&gt;(value, null);
              first.setNext(null);
          }//Otherwise loop to end of list and add new node.
          else {
              while (temp.getNext() != null) {
                  temp = temp.getNext();
              }
              temp.setNext(new ListNode&lt;E&gt;(value, null));
          }
      }//end addLast
  
      // throws an exception - you decide when and which one
  
      public E getLast() {
          ListNode&lt;E&gt; temp = first;
          if (temp == null) {
              throw new NullPointerException("There are no elements in this list to get.");
          } else {
              while (temp.getNext() != null) {
                  temp = temp.getNext();
              }
              return temp.getData();
          }
      }
  
      // throws an exception - you decide when and which one
  
      public E removeFirst() {
          if (first == null) {
              throw new NullPointerException("There are no elements in this list to remove.");
          }
          ListNode&lt;E&gt; tempRemove = first;
          return null; //just so it'll compile
      }
  
      // throws an exception - you decide when and which one
  
      public E removeLast() {
          return null; //just so it'll compile
      }
  
      // return the number of elements in the list
  
      public int size() {
          return 0; //just so it'll compile
      }
  
      // return true if o is in this list, otherwise false
  
      public boolean contains(E obj) {
          return true; //just so it'll compile
      }
  
      public void printList(java.io.PrintStream out) {
          if (first == null) {
              System.out.println("The list is empty");
          }
          ListNode&lt;E&gt; current = first;
          while (current != null) {
              System.out.println(current.toString());
              current = current.getNext();
          }
      }
  
      public String toString() {
          String s = "[";
          ListNode&lt;E&gt; current = first;
          //write code to traverse the list, adding each object on its own line
          while (current.getNext() != null) {
              current = current.getNext();
          }
  
          s += "]";
          return s;
      }
  
      // OPTIONAL: just for fun...and a challenge
  
      public void reverse() {
      }
  }
  </code></pre>
  
  <p>ListNode class is your basic getNext setNext, getData setData.... </p>