The error I get is: FClass.java:53: error: incompatible types: int[] cannot be converted to Iterator Iterator i = sObj.get_Iterator(); => 1 error
I will provide the code for better context, and all I can add is that I need a way to make an Iterator Object and return it.
import java.util.*;
interface Iterator{
public boolean has_next();
public Object get_next();
}
class Sequence{
private final int maxLimit = 80;
private SeqIterator _iter = null;
int[] iArr;
int size;
Sequence(int size){
this.size = size;
}
//implement addTo(elem) to add an int elem to the sequence
public void addTo(int elem){
iArr[iArr.length - 1] = elem;
}
//implement get_Iterator() to return Iterator object
public int[] get_Iterator(){
return iArr;
}
private class SeqIterator implements Iterator{
int indx;
public SeqIterator(){
indx = -1;
}
//implement has_next()
public boolean has_next(){
indx = indx + 1;
if(indx+1 <= size-1){
return true;
}
else{
return false;
}
}
//implement get_next()
public Object get_next(){
return iArr[indx];
}
}
}
class FClass{
public static void main(String[] args) {
Sequence sObj = new Sequence(5);
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 5; i++) {
sObj.addTo(sc.nextInt());
}
Iterator i = sObj.get_Iterator();
while(i.has_next())
System.out.print(i.get_next() + ", ");
}
}