I am supposed to create a simulation that does what pythons xrange does which is pick a start number, stop number, and step number in java. I am having trouble passing my constructor numbers to the Range class as it gives me an Exception in thread "main" java.lang.StackOverflowError. Everytime I step thru the program The Range constructor changes back to "Rane@365", I'm not sure what It means but my numbers I provide are gone.
package com.company;
public class Main {
public static void main(String[] args) {
for (Integer num: new Range(1,10,2)) {
System.out.println(num++);
}
}
}
This is my Range class that uses interfaces Iterator and interface Iterable
package com.company;
import java.util.Iterator;
public class Range implements Iterable<Integer>, Iterator<Integer> {
public Range(int start, int stop, int step){
start = start;
stop = stop;
step = step;
}
public Iterator<Integer> iterator() {
return this;
}
public boolean hasNext() {
if (!this.hasNext()) {
return true;
} else
return false;
}
public Integer next() {
if (!hasNext()) {
throw new IllegalStateException("No next");
}
return null;
}
public void remove() {
}
}
The stuff I have read uses ArrayLists, and I havent found anything on constructors and using loops for the Iterator and Iterable interfaces. I know the hasNext, next, remove are built in methods, where would the loop be? In the Main class or in one of the built in classes?