From 54c717d48cc5408be4d7960771dfabc1d7f486e6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 5 Apr 2018 18:56:02 +0200 Subject: [PATCH] exo2: make the class methods more inline with the List interface. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- exo2/ListExtension.java | 18 +++++++++++------- exo2/Main.java | 16 ++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/exo2/ListExtension.java b/exo2/ListExtension.java index 3ba71ac..a015be5 100644 --- a/exo2/ListExtension.java +++ b/exo2/ListExtension.java @@ -32,28 +32,32 @@ public class ListExtension { return list.add(e); } + public int size() { + return list.size(); + } + /** * Should mimic the List add(int index, T value) method * @param value [description] * @return [description] */ - public void addMiddleIter(E value) { - int mid = list.size() / 2; + public void addIter(int index, E value) { ListIterator iter = list.listIterator(); int i = 0; - // go to the element at mid + 1 index - while (iter.hasNext() && i <= mid) { + // go to the element at index + 1 + while (iter.hasNext() && i <= index) { iter.next(); i++; } iter.add(value); } - public void addNEMiddle(int Nelements) { + //FIXME: replace parameter by the list of objects to add + public void addNelements(int index, int Nelements) { ListIterator iter = list.listIterator(); int i = 0; - // go to the element at the middle + 1 index - while (iter.hasNext() && i <= list.size() / 2) { + // go to the element at index + 1 + while (iter.hasNext() && i <= index) { iter.next(); i++; } diff --git a/exo2/Main.java b/exo2/Main.java index 2df8729..2fbf94f 100644 --- a/exo2/Main.java +++ b/exo2/Main.java @@ -18,20 +18,20 @@ class Main { } long startTime = System.nanoTime(); - //for (int i = 0; i < 100000; i++) { - // array.addMiddleIter(i); - //} - array.addNEMiddle(100000); + // for (int i = 0; i < 100000; i++) { + // array.addIter(array.size() / 2, i); + // } + array.addNelements(array.size() / 2, 100000); long stopTime = System.nanoTime(); long execTimeArray = stopTime - startTime; //System.out.println("Insert time in the middle of the ArrayList: " + TimeUnit.NANOSECONDS.toSeconds(execTime) + " s"); System.out.println("Insert time in the middle of the ArrayList: " + execTimeArray + " ns"); startTime = System.nanoTime(); - //for (int i = 0; i < 100000; i++) { - // list.addMiddleIter(i); - //} - list.addNEMiddle(100000); + // for (int i = 0; i < 100000; i++) { + // list.addIter(list.size() / 2, i); + // } + list.addNelements(list.size() / 2, 100000); stopTime = System.nanoTime(); long execTimeLinked = stopTime - startTime; //System.out.println("Insert time in the middle of the LinkedList: " + TimeUnit.NANOSECONDS.toSeconds(execTime) + " s"); -- 2.34.1