0

I am trying to create my own iterator that loops through an ArrayList of Menu objects which is comprised of MenuItems. Each menuItem has 4 values. I am trying to iterate through the arrayList and only return the values that have the category value MainDish. I keep getting an infinite loop. It has to be in the next() method of my iterator which implements the iterator interface, but I cannot for the life of me find where the error is. It has to be the location of where I increment currentIndex, but can't figure it out. Any and all help is appreciated.

Iterator Class:

package menu;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;


public class ItemIterator implements Iterator<MenuItem> {
private ArrayList<MenuItem> menuList;
private int currentIndex;
private String type;

public ItemIterator(ArrayList<MenuItem> menuList, String type) {
    this.menuList = menuList;
    this.type = type;
    }

@Override
public boolean hasNext() {
    return !(menuList.size() == currentIndex);
}

@Override
public MenuItem next() {

boolean found = false;

    if(hasNext() && !found)
        if(menuList.get(currentIndex).getCategory().equals(type))   
            found = true;
        else
            currentIndex++;         


    if(found = true)
        return menuList.get(currentIndex);
    else
        throw new NoSuchElementException();
    }   


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

}

 }

here is my main:

     public static void main(String[] args) {


    MenuItem item1 = new MenuItem("burger", mainDish, false, 10);
    MenuItem item2 = new MenuItem("sandwhich", appetizer, true, 5);

    Menu newMenu = new Menu();

    newMenu.add(item1);
    newMenu.add(item2);




     Iterator<MenuItem> itr = newMenu.getMenuIterator(); 
     System.out.println("ALL MENU ITEMS"); 


     while (itr.hasNext()) 
     { 
     System.out.println(itr.next()); 
     } 

    itr = newMenu.getItemIterator(mainDish); 
     System.out.println("ALL MAIN DISH ITEMS"); 

     while (itr.hasNext()) 
     { 
     System.out.println(itr.next()); 
     } 
  }
3
  • if(found = true) <-- here. Should be ==. Or just if (found). Commented Feb 21, 2014 at 5:59
  • Why use an iterator? You can do it much more simply with the user of for loops? Commented Feb 21, 2014 at 6:04
  • Ah, changing it to just if(found) helped, but now I get the no such element exception I had it push to as the default case. thoughts? Commented Feb 21, 2014 at 6:17

1 Answer 1

1

The next() method needs to increment currentIndex whether or not the current item is a match. As written, you get an infinite loop as soon as you reach an actual match.

Incidentally, as written, there's no reason not to use the regular iterator and check the result. If what you really want is an iterator that skips ahead to the next match, you'll need to add an increment loop inside the next() method.

Sign up to request clarification or add additional context in comments.

6 Comments

I am now getting the no such element exception i had it go to as the default case.
Right now as you have it written, you will get NoSuchElementException whenever the current element is not a match and you call next().
I guess my next task is to figure out where that looping structure goes
I can't seem to figure out how to place the loop.
You could start by replacing the if(hasNext() && !found) with while(hasNext() && !found). You'll still need to avoid throwing NoSuchElementException when the iterator is at a point after the last match but before the end, though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.