From 229c68282dc8bc3cbd9b8c6a37f6fa1dcd378da0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 5 Apr 2018 14:32:56 +0200 Subject: [PATCH] exo2: simplify iterator loops. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- exo2/ListExtension.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/exo2/ListExtension.java b/exo2/ListExtension.java index bba85d2..3ba71ac 100644 --- a/exo2/ListExtension.java +++ b/exo2/ListExtension.java @@ -41,24 +41,22 @@ public class ListExtension { int mid = list.size() / 2; ListIterator iter = list.listIterator(); int i = 0; - // go to the element at mid index - while (iter.hasNext() && i < mid) { + // go to the element at mid + 1 index + while (iter.hasNext() && i <= mid) { iter.next(); i++; } - iter.next(); // Insert before mid + 1 iter.add(value); } public void addNEMiddle(int Nelements) { ListIterator iter = list.listIterator(); int i = 0; - // go to the element at the middle index - while (iter.hasNext() && i < list.size() / 2) { + // go to the element at the middle + 1 index + while (iter.hasNext() && i <= list.size() / 2) { iter.next(); i++; } - iter.next(); // Insert before mid + 1 for (int j = 0; j < Nelements; j++) { iter.add((E)new Object()); } -- 2.34.1