From cb86aaefb7d780cacbcd6c618ff81cc320712ee4 Mon Sep 17 00:00:00 2001 From: Guyslain <guyslain.naves@lis-lab.fr> Date: Mon, 23 Oct 2023 15:49:27 +0200 Subject: [PATCH] section 3 CoordinateIterator fini --- .../java/datastruct/CoordinateIterator.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/datastruct/CoordinateIterator.java b/src/main/java/datastruct/CoordinateIterator.java index 37360d0..d9e87d3 100644 --- a/src/main/java/datastruct/CoordinateIterator.java +++ b/src/main/java/datastruct/CoordinateIterator.java @@ -9,6 +9,10 @@ import java.util.NoSuchElementException; */ class CoordinateIterator implements Iterator<Coordinate> { + private Coordinate current = new Coordinate(0,0); + private final int width; + private final int height; + /** * Creates a new {@link CoordinateIterator} with the specified width and height. * @@ -16,7 +20,8 @@ class CoordinateIterator implements Iterator<Coordinate> { * @param height The height of the coordinate range. */ public CoordinateIterator(int width, int height) { - // TODO: à compléter + this.width = width; + this.height = height; } /** @@ -26,8 +31,7 @@ class CoordinateIterator implements Iterator<Coordinate> { */ @Override public boolean hasNext() { - // TODO: à compléter - return false; + return this.current.y() != height; } /** @@ -38,7 +42,18 @@ class CoordinateIterator implements Iterator<Coordinate> { */ @Override public Coordinate next() { - // TODO: à compléter - return null; + if (!hasNext()) { + throw new NoSuchElementException(); + } + Coordinate next = this.current; + this.update(); + return next; + } + + private void update() { + this.current = this.current.right(); + if (this.current.x() == this.width) { + this.current = new Coordinate(0,this.current.y() + 1); + } } } -- GitLab