## Tuesday, March 29, 2016

### Itertors

https://xmruibi.gitbooks.io/interview-preparation-notes/content/DataStructure/Iterator/JumpIterator.html
Implements an iterator in each output it print the number and skip the next one.
public class JumpIterator implements Iterator<Integer> {

Iterator<Integer> iterator;

public JumpIterator(Iterator<Integer> iterator) {
this.iterator = iterator;
}

@Override
public boolean hasNext() {
return iterator.hasNext();
}

@Override
public Integer next() {
int res = iterator.next();
if (iterator.hasNext())
iterator.next();
return res;
}
}

+
https://xmruibi.gitbooks.io/interview-preparation-notes/content/DataStructure/Iterator/EvenIterator.html
Implements an iterator only output the even number.
public class EvenIterator implements Iterator<Integer> {

Iterator<Integer> iterator;

public EvenIterator(Iterator<Integer> iterator) {
this.iterator = iterator;
}

@Override
public boolean hasNext() {
return iterator.hasNext(); // should put while logic here
}

@Override
public Integer next() {
int res = 0;
while (iterator.hasNext() && (res = iterator.next()) % 2 != 0)
;
return res;
}
}

https://xmruibi.gitbooks.io/interview-preparation-notes/content/DataStructure/Iterator/IteratorFor2DArray.html
Set up a iterator to iterate a 2-dimensional array.
Iterator for 2-D Array
• Iterate the 2-D array by x and y.
• Record current element during hasNext() function.
• Check the row if it is null.
public class DeepIterator{
int cur; // this is important
int row = 0, col = 0;

int[][] listOfLists;

public DeepIterator(int[][] listOfLists){
if(listOfLists == null)
throw new IllegalArgumentException("Null Input");
this.listOfLists = listOfLists;
}

public Integer next(){
return cur;
}

public boolean hasNext(){
// make sure the row is not null
while(row < listOfLists.length && col >= listOfLists[row].length) {
row ++; col = 0;
}
if(row < listOfLists.length) {
cur = listOfLists[row][col++];
return true;
}else
return false;
}

public static void main(String[] args) {
int[][] listOfLists = {
{},{},{1,2,3},{},{},{2,3,4}
};
DeepIterator it = new DeepIterator(listOfLists);
while(it.hasNext()){
System.out.println(it.next());
}
}
}
https://xmruibi.gitbooks.io/interview-preparation-notes/content/DataStructure/Iterator/IteratorOfIterators.html
Given a Iterator which contain several iterator inside. The task is to iterate all elements inside these iterators
• It's a class implements iterator interface but also contains several iterators.
• Image these iterator is a bucket, the unit is a iterator and the cursor of bucket index is a iteratorelement.
• Set a current index to point at a iterator.
public class Iterators<T> implements Iterator<T>{
Iterators<T> current;
Iterators<Iterators<T>> cursor;
public Iterators(Iterable<Iterator<T>> iterators){
if(iterators == null)
throw new IllegalArgumentException("Illegal Argument!");
this.cursor = iterators;
}

@Override
public T next(){
return current.next();
}

@Override
public boolean hasNext(){
if(!current.hasNext())
current = findNext();
return current != null && current.hasNext();
}

private Iterator findNext(){
while(cursor.hasNext()){
current = cursor.next();
if(current != null && current.hasNext())
break;
}
return current;
}

@Override
public void remove(){
if(current!=null)
current.remove();
}
}
https://xmruibi.gitbooks.io/interview-preparation-notes/content/DataStructure/Iterator/PeekIterator.html
Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be returned by the next call to next().

+
class PeekingIterator implements Iterator<Integer> {

int cur;
Iterator<Integer> it;
public PeekingIterator(Iterator<Integer> iterator) {
this.it = iterator;
cur = it.hasNext() ? it.next() : null; //\\
}

// Returns the next element in the iteration without advancing the iterator.
public Integer peek() {
return cur;
}

// hasNext() and next() should behave the same as in the Iterator interface.
// Override them if needed.
@Override
public Integer next() {
int res = curl
cur = it.next() ? it.next() : null;
return res;
}

@Override
public boolean hasNext() {
return it.hasNext();
}
}