Jakob Nielsen
Jakob Nielsen

Reputation: 5198

Having troubles creating an iterator for my own list

My list looks like this:

public class SList<A> implements Iterable<A> 
{
    private Listelem head;
    private Listelem current;
    private boolean listEmpty;

    private class Listelem 
    {
        private A value;
        private Listelem next;

        private Listelem(A val) 
        {
            this.value = val;
            this.next  = null;
        }
        private Listelem() 
        {
            this.next  = null;
        }

        public void setValue(A val) 
        {
            this.value = val;
        }
        public A getValue() 
        {
            return this.value;
        }
        public void setSuccessor(Listelem next)
        {
            this.next = next;
        }
        public Listelem getSuccessor() 
        {
            return this.next;
        }
    }
}

I want to create an Iterator for this List but I am having some troubles. In SList I am doing this:

@Override
public Iterator<A> iterator() {
    Iterator<A> it = new Iterator<A>() {

        this.current = this.head;

        @Override
        public boolean hasNext() {
           boolean hasNext = true;
           if( this.current.getSucessor == null )
           {
               hasNext = false;
           }
           return hasNext;
        }

        @Override
        public A next() {
            A next       = this.current.getValue;
            this.current = this.current.getSuccessor();
            return next;
        }

        @Override
        public void remove() {
            // TODO Auto-generated method stub
        }
    };
    return it;
}

I can`t reference this.current or this.head. I am wondering why this is not working, since I am in the same class.

Upvotes: 0

Views: 105

Answers (3)

JB Nizet
JB Nizet

Reputation: 691635

You simply forgot to declare a current field in your Iterator. And the head of the list should be accessed with SList.this.head, or simply with head. this refers to the Iterator instance. Not to the list. You should use a non-anonymous class:

@Override
public Iterator<A> iterator() {
    return new MyListIterator();
}

private class MyListIterator implements Iterator<A> {
    private Listelem current;

    private MyListIterator() {
        this.current = head;
    }

    @Override
    public boolean hasNext() {
       return this.current.getSucessor != null;
    }

    @Override
    public A next() {
        A next       = this.current.getValue;
        this.current = this.current.getSuccessor();
        return next;
    }

    @Override
    public void remove() {
        // TODO Auto-generated method stub
    }
}

Upvotes: 1

Andrei Nicusan
Andrei Nicusan

Reputation: 4623

Try SList.this.head. You're attempting to reference a field that doesn't exist in the Iterator subclass you're defining.

Instead, you want to reference the head field of the enclosing SList class. That's what you can obtain by using the snippet I posted in the beginning.

Upvotes: 0

Blub
Blub

Reputation: 3822

You are creating a new Iterator with new, therefore you are in a anonymous inner class of your class. Try with SList.this.current.

Upvotes: 1

Related Questions