From: Jérôme Benoit Date: Thu, 5 Apr 2018 12:32:56 +0000 (+0200) Subject: exo2: simplify iterator loops. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=229c68282dc8bc3cbd9b8c6a37f6fa1dcd378da0;p=Project_POO.git exo2: simplify iterator loops. Signed-off-by: Jérôme Benoit --- 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()); }