简体   繁体   中英

Iterator Infinite Loop - hasNext() and Next()

I'm trying to use an iterator that implements Iterator. The iterator is supposed to go through a hash table. When I try to print out the elements in the hashtable, I get an infinite loop somewhere and the same element keeps being printed until I terminate the program. This is my code for my hasNext and next methods (the cursor keeps track of the next active cell in the hashtable as the table will not be filled in order, and active means that the cell is being occupied):

public boolean hasNext()
    {
        boolean entry = false;
        //nextLoop:
        while(entry == false && cursor < table.length)
        {
            if(table[cursor] == null)
            {
                cursor++;
            }
            else
            {
                if(table[cursor].active == false)
                {
                    cursor++;
                }
                else
                {
                    entry = true;
                    //break nextLoop;
                }
            }
        }
        boolean entryStatus = (table[cursor] != null); // check to see if entry at cursor is null
        boolean activeStatus = table[cursor].active; // check to see if the cell is active (there is something inside the cell)

        return (entryStatus && activeStatus);
    }

    public Object next()
    {
        boolean entry = false;
        if(cursor >= table.length)
        {
            throw new NoSuchElementException(); //check - myexceptioN?
        }
        else
        {

            while(cursor < table.length && entry == false) 
            {
                if(table[cursor] != null) 
                {
                    if(table[cursor].active == true)
                    {
                        entry = true;
                    }
                    else
                    {
                        cursor++;
                    }
                }
                else if(table[cursor] == null)
                {
                    cursor++;
                }
            }


        }
        return table[cursor].element;
    }

If you have an element then the next() method should return the element under that cursor (as it does) and then update the cursor (which is doesn't). So your code always stays to the same element since hasNext will be called with the same cursor position.

You need to move the cursor to the next position after you get the value in the next method.

As the user Ryan J said in the comments, you should work hand-in-hand your next() and hasNext() methods. And you need increment the cursor after calling next.

    public Object next() {
        if (cursor >= table.length  || table[cursor].active == true 
             || table[cursor] == null) {
            throw new NoSuchElementException(); 
        }

        return table[cursor++].element;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM