Created
August 20, 2018 07:46
-
-
Save Phoenix124/a86db6f3fc8c13bab2db6b7360a616c1 to your computer and use it in GitHub Desktop.
Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
public class Converter { | |
Iterator<Integer> convert(Iterator<Iterator<Integer>> it) { | |
return new Iterator<Integer>() { | |
/** | |
* Current step of iterator | |
*/ | |
Iterator<Integer> current = it.next(); | |
/** | |
* Check next step of iterator | |
* @return true or false | |
*/ | |
@Override | |
public boolean hasNext() { | |
boolean hasNext = false; | |
if (current.hasNext()) { | |
hasNext = true; | |
} else { | |
while (it.hasNext()) { | |
this.current = it.next(); | |
if (current.hasNext()) { | |
hasNext = true; | |
break; | |
} | |
} | |
} | |
return hasNext; | |
} | |
/** | |
* Check next int in iterator and return it, else go to next iterator and return int of it | |
* @return next value of iterator | |
*/ | |
@Override | |
public Integer next() { | |
if (!this.hasNext()) { | |
throw new NoSuchElementException(); | |
} | |
return current.next(); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment