Singly Linked List implementation in Java Iterator
<p>I'm writing a simple implementation of a Bag in Java. I am implementing <code>Iterable</code> and writing my own <code>LinkedList</code> Iterator. So I'm racking my brain; I am trying to add elements to the linked list. I have a working implementation (the commented out code in the <code>add()</code> function). However, I do not understand why the following code does not work:</p>
<pre><code>current.item = item;
Node&lt;T&gt; nextNode = new Node&lt;T&gt;();
current.next = nextNode;
current = nextNode;
</code></pre>
<p>So, given that the list is empty and the current head is initialized but has no item or next: I assign item to the current item, create a new node, set it to the current's next and change the current (head) to the node I just created. Adding two items to the list, I printed out the objects for posterity:</p>
<blockquote>
<p>current: Bag$Node@4524411f next: <strong>Bag$Node@401e7803</strong></p>
<p>current: <strong>Bag$Node@401e7803</strong> next: Bag$Node@10dba097</p>
<p>current: Bag$Node@10dba097 next: <strong>Bag$Node@1786f9d5</strong></p>
<p>current: <strong>Bag$Node@1786f9d5</strong> next: Bag$Node@704d6e83</p>
</blockquote>
<p>It looks clearly, to me at least, that the next is getting set with a new node each time just fine. I get all four elements added to the bag, but the item is lost and returns null for each index. The toArray() function shows <code>[null, null, null, null]</code></p>
<p>I'm sure it's something blindingly simple. Below is the entire implementation.</p>
<pre><code>import java.util.Iterator;

public class Bag&lt;T&gt; implements Iterable&lt;T&gt; {
    private Node current;
    //Node&lt;T&gt; head;
    private int numberofProducts;
    T[] myBag;
    int defaultCapacity;

    public Iterator&lt;T&gt; iterator() {
        return new ListIterator&lt;T&gt;(current);
    }

    public Bag(int defaultCapacity) {
        this.current = new Node&lt;T&gt;();
        this.numberofProducts = 0;
        this.defaultCapacity = defaultCapacity;
    }

    public void add(T item) {
        if(isFull()) {
            System.out.println(&quot;bags full, yo&quot;);
            return;
        }

        current.item = item;
        Node&lt;T&gt; nextNode = new Node&lt;T&gt;();
        current.next = nextNode;
        current = nextNode;

        numberofProducts++;

    //Node&lt;T&gt; nextNode = current;
    //current = new Node&lt;T&gt;();
    //current.item = item;
    //current.next = nextNode;
    //numberofProducts++;


    }

    public Object[] toArray() {
        Object[] array = new Object[size()];

        int i = 0;
        Node&lt;T&gt; node = current;
        //Node&lt;T&gt; node = head;
        while(node.next != null) {
            array[i] = node.item;
            node = node.next;
            i++;
        }

        return array;
    }

    public boolean isEmpty() {
        return this.numberofProducts &lt;= 0;
    }

    public boolean isFull() {
        return this.numberofProducts &gt;= defaultCapacity;
    }

    public int size() {
        return this.numberofProducts;
    }

    private class Node&lt;T&gt; {
        private T item;
        private Node&lt;T&gt; next;
    }


    private class ListIterator&lt;T&gt; implements Iterator&lt;T&gt; {

        private Node&lt;T&gt; current;

        public  ListIterator(Node&lt;T&gt; first) {
            current = first;
        }

        public boolean hasNext() {

            return current != null;
        }

        public T next() {
            if(hasNext()) {
                T item = current.item;
                current = current.next;
                return item;
            }
            return null;
        }

        public void remove() {

        }
    }
}
</code></pre>