Skip to content
Snippets Groups Projects
Select Git revision
  • 3bc378d7c54a3f325039aa00712fb552ff192ab9
  • main default protected
2 results

ArrayGridTest.java

Blame
  • Forked from TRAVERS Corentin / flooding-template
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    CoordinateIterator.java 814 B
    package datastruct;
    
    import java.util.Iterator;
    import java.util.NoSuchElementException;
    
    class CoordinateIterator implements Iterator<Coordinate> {
        private final int width;
        private final int height;
        private int x = 0;
        private int y = 0;
    
        public CoordinateIterator(int width, int height) {
            this.width = width;
            this.height = height;
        }
    
        @Override
        public boolean hasNext() {
            return y < this.height;
        }
    
        @Override
        public Coordinate next() {
            if (!this.hasNext()) {
                throw new NoSuchElementException();
            }
            Coordinate coord = new Coordinate(this.x, this.y);
            this.x = this.x + 1;
            if (this.x == this.width) {
                this.x = 0;
                this.y = this.y + 1;
            }
            return coord;
        }
    }