Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Enumeration;
- import java.util.NoSuchElementException;
- /** Iterator for maps.
- * Iterates over all none-border elements of the map.
- */
- class MapIterator implements Enumeration {
- /** The map to iterate over */
- private Map map;
- /** Current row */
- private int row = 1;
- /** Current column */
- private int col = 0;
- /** Create an iterator given a map */
- MapIterator(Map map) {
- this.map = map;
- }
- /** Check if there are more elements to iterate */
- public boolean hasMoreElements() {
- return (row < map.height-2 && col < map.width-2);
- }
- /** Return the next element */
- public Element nextElement() throws NoSuchElementException {
- if (!hasMoreElements()) {
- throw new NoSuchElementException();
- }
- if (col == map.width-2) {
- row += 1;
- col = 1;
- }
- else {
- col += 1;
- }
- return map.getElement(row, col);
- }
- public int getRow() { return row; }
- public int getCol() { return col; }
- }
Add Comment
Please, Sign In to add comment