Guest User

Untitled

a guest
May 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. import java.util.Enumeration;
  2. import java.util.NoSuchElementException;
  3.  
  4. /** Iterator for maps.
  5.  * Iterates over all none-border elements of the map.
  6.  */
  7. class MapIterator implements Enumeration {
  8.     /** The map to iterate over */
  9.     private Map map;
  10.     /** Current row */
  11.     private int row = 1;
  12.     /** Current column */
  13.     private int col = 0;
  14.  
  15.     /** Create an iterator given a map */
  16.     MapIterator(Map map) {
  17.         this.map = map;
  18.     }
  19.  
  20.     /** Check if there are more elements to iterate */
  21.     public boolean hasMoreElements() {
  22.         return (row < map.height-2 && col < map.width-2);
  23.     }
  24.  
  25.     /** Return the next element */
  26.     public Element nextElement() throws NoSuchElementException {
  27.         if (!hasMoreElements()) {
  28.             throw new NoSuchElementException();
  29.         }
  30.  
  31.         if (col == map.width-2) {
  32.             row += 1;
  33.             col = 1;
  34.         }
  35.         else {
  36.             col += 1;
  37.         }
  38.  
  39.         return map.getElement(row, col);
  40.     }
  41.    
  42.     public int getRow() { return row; }
  43.     public int getCol() { return col; }
  44. }
Add Comment
Please, Sign In to add comment