-3

I have this interface:

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

and this class:

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;
    }  
}

and this class:

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();
    }
}

as you can see Experiment class implements Iterable inteface so I can use foreach.

but when I try to use foreach on Experiment object:

     Experiment experiment1 = new Experiment(5);

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

I get this error:

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)

what is wrong with my code?What I do wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Michael
  • 13,950
  • 57
  • 145
  • 288
  • 5
    `return this.iterator();` - what do you imagine this does? – Oliver Charlesworth Jul 16 '17 at 18:58
  • 3
    Surely you've been here long enough to know that you should really be making a debugging attempt before asking questions like this here. – Joe C Jul 16 '17 at 19:00
  • On that note, it appears this is the latest in a flurry of low-effort questions that are all being downvoted. My understanding is that this will eventually lead to being blocked from SO if it goes on for much longer. – Oliver Charlesworth Jul 16 '17 at 19:01

2 Answers2

3

Your method Experiment.iterator is calling itself in an infinite recursion.

If you are using Java 8 you could do:

@Override
public Iterator iterator() { Arrays.stream(chains).iterator(); }
Mikel San Vicente
  • 3,831
  • 2
  • 21
  • 39
1

You need to return an implementation of the interface Iterator in your implementation of the iterator method. Also you should specify the generic type of the iterator in your class declaration.

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

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

    };
}

Edit: Actually in your case you should return the experiment itself since it's the class that implements iterator... which is bad! Create an inner class that implements your ExpIterator. Also just for info, variable names should start with a lower case by convention (DNAChain[] chains).

It should be something like this:

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(); }
}

When your Experiment is fully set, retrieve the iterator to go through your DNA chains.

Maaaatt
  • 429
  • 4
  • 14