h*********o 发帖数: 230 | 1 Implement peek() and pop() from java iterator().
for example [1,2,3,4,5], peek() = 1, pop() = 1, peek() = 2, peek() = 2, pop(
) = 2 | p*****2 发帖数: 21240 | 2 class MyIterator[T](it:Iterator[T]){
private[this] var curr:T=_
private[this] var has=false
def peek():T={
if(has) return curr
if(it.hasNext){has=true; curr=it.next; curr} else null.asInstanceOf[
T]
}
def pop():T={
if(has) has=false; return curr
if(it.hasNext) it.next else null.asInstanceOf[T]
}
} |
|