简体   繁体   English

通过ArrayList Java进行无限循环迭代

[英]Infinite Loop iterating through ArrayList Java

I am trying to create my own iterator that loops through an ArrayList of Menu objects which is comprised of MenuItems. 我试图创建自己的迭代器,该迭代器遍历由MenuItems组成的Menu对象的ArrayList。 Each menuItem has 4 values. 每个menuItem具有4个值。 I am trying to iterate through the arrayList and only return the values that have the category value MainDish. 我试图遍历arrayList,只返回具有类别值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. 它必须在实现迭代器接口的迭代器的next()方法中,但是我终生无法找到错误所在。 It has to be the location of where I increment currentIndex, but can't figure it out. 它必须是我增加currentIndex的位置,但无法弄清楚。 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()); 
     } 
  }

The next() method needs to increment currentIndex whether or not the current item is a match. 无论当前项目是否匹配,next()方法都需要使currentIndex递增。 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. 如果您真正想要的是一个可以直接跳到下一个匹配项的迭代器,则需要在next()方法内添加一个增量循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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