X-Git-Url: https://git.piment-noir.org/?p=Project_POO.git;a=blobdiff_plain;f=exo2%2FMain.java;h=2df8729213d967716022dd7cc45a31b9727272f9;hp=85d48b512ee127807662fae64d40d5a6bdbbd53f;hb=01bad5b37bf95f027f583e1b1da607e074050651;hpb=4845756e424e8a5e3ebc17d089d04dc42a936899 diff --git a/exo2/Main.java b/exo2/Main.java index 85d48b5..2df8729 100644 --- a/exo2/Main.java +++ b/exo2/Main.java @@ -1,94 +1,16 @@ import java.util.ArrayList; import java.util.LinkedList; -import java.util.ListIterator; import java.util.concurrent.TimeUnit; class Main { - private static void addMiddle(ArrayList array, E value) { - int mid = array.size() / 2; - array.add(mid + 1, value); - } - - private static void addMiddle(LinkedList list, E value) { - int mid = list.size() / 2; - list.add(mid + 1, value); - } - - /** - * Should mimic the collection add(int index, T value) method - * @param array [description] - * @param value [description] - * @return [description] - */ - private static void addMiddleIter(ArrayList array, E value) { - int mid = array.size() / 2; - ListIterator iter = array.listIterator(); - int i = 0; - // go to the element at mid index - while (iter.hasNext() && i < mid) { - iter.next(); - i++; - } - iter.next(); // Insert before mid + 1 - iter.add(value); - - } - - /** - * Should mimic the collection add(int index, T value) method - * @param list [description] - * @param value [description] - * @return [description] - */ - private static void addMiddleIter(LinkedList list, E value) { - int mid = list.size() / 2; - ListIterator iter = list.listIterator(); - int i = 0; - // go to the element at mid index - while (iter.hasNext() && i < mid) { - iter.next(); - i++; - } - iter.next(); // Insert before mid + 1 - iter.add(value); - } - - private static void addNEMiddle(ArrayList array, int Nelements) { - ListIterator iter = array.listIterator(); - int i = 0; - // go to the element at the middle index - while (iter.hasNext() && i < array.size() / 2) { - iter.next(); - i++; - } - iter.next(); // Insert before mid + 1 - for (int j = 0; j < Nelements; j++) { - iter.add((E)new Object()); - } - } - - private static void addNEMiddle(LinkedList list, int Nelements) { - ListIterator iter = list.listIterator(); - int i = 0; - // go to the element at the middle 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()); - } - } - /** * The main() function * @param String[] args main() function arguments array */ public static void main(String[] args) { - ArrayList array = new ArrayList(); - LinkedList list = new LinkedList(); + ListExtension array = new ListExtension(new ArrayList()); + ListExtension list = new ListExtension(new LinkedList()); for (int i = 0; i < 1000000; i++) { array.add(i); @@ -97,22 +19,23 @@ class Main { long startTime = System.nanoTime(); //for (int i = 0; i < 100000; i++) { - // addMiddleIter(array, i); + // array.addMiddleIter(i); //} - addNEMiddle(array, 100000); + array.addNEMiddle(100000); long stopTime = System.nanoTime(); - long execTime = stopTime - startTime; + 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: " + execTime + " ns"); + System.out.println("Insert time in the middle of the ArrayList: " + execTimeArray + " ns"); startTime = System.nanoTime(); //for (int i = 0; i < 100000; i++) { - // addMiddleIter(list, i); + // list.addMiddleIter(i); //} - addNEMiddle(list, 100000); + list.addNEMiddle(100000); stopTime = System.nanoTime(); - execTime = stopTime - startTime; + long execTimeLinked = stopTime - startTime; //System.out.println("Insert time in the middle of the LinkedList: " + TimeUnit.NANOSECONDS.toSeconds(execTime) + " s"); - System.out.println("Insert time in the middle of the LinkedList: " + execTime + " ns"); + System.out.println("Insert time in the middle of the LinkedList: " + execTimeLinked + " ns"); + System.out.println("Rapport du temps d'execution ArrayList/LinkedList = " + (execTimeArray / execTimeLinked)); } }