Question: help with this code, output supoose to be ( 1 , 2 , 2 , 3 , false ) and ( 1 0 0 ,

help with this code, output supoose to be (1,2,2,3,false) and (100,null,null,false)
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
public class MyIterator implements Iterator {
private List list;
private int index;
public MyIterator(Iterator iterator){
this.list = new ArrayList<>();
while(iterator.hasNext()){
this.list.add(iterator.next());
}
this.index =0;
}
public Integer lookAhead(){
if(index < list.size()-1){
return list.get(index+1);
}
return null;
}
@Override
public boolean hasNext(){
return index < list.size();
}
@Override
public Integer next(){
if(!hasNext()){
return null;
}
Integer element = list.get(index);
index++;
return element;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class HW7_1{
public static void main(String[] args){
List ints = new ArrayList<>(Arrays.asList(1,2,3));
MyIterator iter = new MyIterator(ints.iterator());
System.out.println(iter.next()); //1
System.out.println(iter.lookAhead()); //2
System.out.println(iter.next()); //2
System.out.println(iter.next()); //3
System.out.println(iter.hasNext()); // false
List(Arrays.asList(100))p
MyIterator iter2= new MyIterator(ints2.iterator());
System.out.println(iter2.next()); //100
System.out.println(iter2.lookAhead()); // null
System.out.println(iter2.next()); // null
System.out.println(iter2.hasNext()); // false
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!