Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 只能在数组上迭代_Java - Fatal编程技术网

Java 只能在数组上迭代

Java 只能在数组上迭代,java,Java,它说:只能迭代java.lang.Iterable的数组或实例 有人能帮我解决这个问题吗 我不知道如何修复这个错误,错误在for循环中 错误消息是正确的,这 enum Bags { SMALL(10832, 10000000), MEDIUM(10833, 100000000), HUGE(10834, 100000000), HEFTY( 10835, 2147000000); public int id, mAmount; Bags(int

它说:
只能迭代java.lang.Iterable的数组或实例

有人能帮我解决这个问题吗


我不知道如何修复这个错误,错误在for循环中

错误消息是正确的,这

enum Bags {
    SMALL(10832, 10000000), MEDIUM(10833, 100000000), HUGE(10834, 100000000), HEFTY(
            10835, 2147000000);

    public int id, mAmount;

    Bags(int id, int mAmount) {
        this.id = id;
        this.mAmount = mAmount;
    }

    public int getId() {
        return id;
    }

    public int getMoneyAmount() {
        return mAmount;
    }

    public Bags getBagConfig(int index) {

        for (Bags bag : Bags.values()) {

            for (int bagId : bag.getId()) {

                if (bagId == index) {
                    return bag;
                }

            }

        }
        return null;
    }
}

public static void init(final Client c, final int itemUsed,
        final int useWith) {
    if (itemUsed == 7 && useWith == 1) {
        c.getItems().deleteItem(7, 1);
    }
    c.eventContainer.addEvent(new CycleEvent(5) {

        public void execute() {
            c.getItems().addItem(1, 3);
        }
    });
}
因为
getId()
返回单个
int

替换此代码:

for (Bags bag : Bags.values()) {
  if (bag.getId() == index) {
    return bag;
  }
}


这是导致问题的第二个for循环。您试图迭代bag.getId(),它只是一个int。因此,我应该将int设置为int[][]?在大量文本中重复这个问题非常糟糕。那么我应该怎么做?使用if语句而不是for循环
for (Bags bag : Bags.values()) {
  if (bag.getId() == index) {
    return bag;
  }
}
for (int bagId : bag.getId()) {
    if (bagId == index) {
         return bag;
    }
}
if (bag.getId() == index) {
     return bag;
}