I tried to work this out but couldn't.
I need to implement a class which implements iterator and takes iterator as constructor parameter,
1)Need to return every 2nd hasnext
2)Need to return every 2nd next element
Basically I am trying to make use of given iterator received from constructor, But when i use next element on hasnext I am actually increasing the iterator by one element. so problem comes when i independently access hasNext or next element and does not pass all the test cases. Any solution or idea on this
Template and my expected implementation looks like below:
public class AlternateIterator<T> implements Iterator<T>
public AlternateIterator(Iterator<T> target)
public boolean hasNext() {
boolean returnvalue = false;
if(iterator.hasNext()) {
iterator.next();
returnvalue = iterator.hasNext();
}
return returnvalue;
}
@Override
public T next() {
T object = null;
if(iterator.hasNext()) {
object = iterator.next();
return object;
}
else
return null;
-- Gone through this link but it creates a new implementation itself while i want to use the given template only: