我使用foreach时为什么会出现StackOverflowError?

时间:2017-07-16 18:55:21

标签: java

我有这个界面:

public interface ExpIterator extends Iterator<DNAChain> {
    void setLevel(int n);
}

和这堂课:

public class DNAChain {
    private  String  Chain;
    private  boolean natural;

    public String getChain() {
        return Chain;
    }

    public  DNAChain (String  Chain, boolean natural) {
        this.Chain = Chain;
        this.natural = natural;
    }  
}

和这堂课:

public class Experiment implements ExpIterator,Iterable{

    private DNAChain[] Chains; // Chains found in the experiment
    private int counter; // next  chain  index

    public DNAChain[] getChains() {
        return Chains;
    }

    public void setChains(DNAChain[] chains) {
        Chains = chains;
    }

    public  Experiment () {}

    public  Experiment (int  n) {
        Chains  = new DNAChain[n];
    }

    @Override
    public void setLevel(int n) {
    }

    @Override
    public boolean hasNext() {
        return counter < Chains.length;
    }

    @Override
    public DNAChain next() {
        return Chains[counter++];
    }

    @Override
    public void remove() {
    }

    @Override
    public Iterator iterator() {
      return this.iterator();
    }
}

正如您所见,Experiment类实现了Iterable inteface,因此我可以使用foreach。

但是当我尝试在实验对象上使用foreach时:

     Experiment experiment1 = new Experiment(5);

     for (Object o :experiment1) {
          System.out.println("ddd");
         ((DNAChain)o).getChain();
       }

我收到此错误:

Exception in thread "main" java.lang.StackOverflowError
    at Experiment.iterator(Experiment.java:72)
    at Experiment.iterator(Experiment.java:72)
    at Experiment.iterator(Experiment.java:72)

我的代码出了什么问题?我做错了什么?

2 个答案:

答案 0 :(得分:3)

您的方法Experiment.iterator在无限递归中调用自身。

如果您使用的是Java 8,则可以:

var temp = "PropertyName";
context.Table.Where(a => a.GetProperty(temp) > 5);

答案 1 :(得分:1)

您需要在迭代器方法的实现中返回接口Iterator的实现。您还应该在类声明中指定迭代器的泛型类型。

@Override
public Iterator<DNAChain> iterator()
{
    return new Iterator<DNAChain>()
    {
        @Override
        public boolean hasNext() { return false; }

        @Override
        public DNAChain next() { return null; }

    };
}

编辑:实际上在你的情况下你应该返回实验本身,因为它是实现迭代器的类......这很糟糕!创建一个实现ExpIterator的内部类。同样只是为了信息,变量名称应该按照约定从小写开始(DNAChain []链)。

它应该是这样的:

public class Experiment implements Iterable
{
    private DNAChain[] chains; // Chains found in the experiment
    private int counter; // next  chain  index

    public  Experiment () {}
    public  Experiment (int  n) { chains  = new DNAChain[n]; }

    public DNAChain[] getChains() { return chains; }
    public void setChains(DNAChain[] chains) { this.chains = chains; }

    public class ExpIteratorImpl implements ExpIterator
    {
        @Override
        public void setLevel(int n) { }

        @Override
        public boolean hasNext() { return counter < chains.length; }

        @Override
        public DNAChain next() { return chains[counter++]; }
    }

    @Override
    public Iterator iterator() { return new ExpIteratorImpl(); }
}

完成实验后,检索迭代器以通过DNA链。