I am new to the whole "iterator" concept in Java and need help with implementing it in my code. Here is the code:
class IteratorExample {
int tstArray [];
IteratorExample(){
}
public void addNum(int num){
tstArray[0] = num; //And yes, I can only add one number at the moment, but it
is not what I want to focus on right now.
}
public Iterator<Integer> innerIterator(){
return new methodIterator();
}
class methodIterator implements Iterator<Integer> {
public int index;
private methodIterator(){
index = 0;
}
public boolean hasNext(){
return index < tstArray.length;
}
public Integer next(){
return;
}
}
public static void main(String[] args){
IteratorExample sample = new IteratorExample();
test(sample);
}
public static void test(IteratorExample arr){
arr.addNum(1);
system.out.print(arr);
}
}
This is the code written so far. I want to make it so I can add a number to an array using the addNum() method and then display it from main using system.print (and yes, I am aware that I need a toString method in order for the numbers to come up instead of the memory-address, that will be implemented later on, right now I am only focused on getting this to work.)