Google Interview Question
1,223 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
Helpful Question?
Yes |
No
Inappropriate?
0 of 0 people found this helpful
by Liron:
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class MyIterator<E> implements Iterator{
int pos;
private Collection<E> coll;
public boolean hasNext() {
return pos < coll.size();
}
public E next() {
if(pos == coll.size()){
throw new NoSuchElementException();
}
return (E) coll.toArray()[pos++];
}
public void remove() {
throw new UnsupportedOperationException();
}
public static void main(String[] args){
Collection<Object> list = new LinkedList<Object>();
list.add(1);list.add(2);list.add(3);
MyIterator iter = new MyIterator();
iter.coll = list;
iter.pos = 0;
while (iter.hasNext()){
System.out.println(iter.next());
}
}
}