X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=exo2%2FMain.java;h=75c828bb2d17edca3f2627bcffaf59226967b55c;hb=e1499f74d5264f2205c54bf9b1a4c66fd447a844;hp=85d48b512ee127807662fae64d40d5a6bdbbd53f;hpb=b42baaebf13977077ead822d691d0787fe562a15;p=Project_POO.git diff --git a/exo2/Main.java b/exo2/Main.java index 85d48b5..75c828b 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); @@ -96,23 +18,24 @@ class Main { } long startTime = System.nanoTime(); - //for (int i = 0; i < 100000; i++) { - // addMiddleIter(array, i); - //} - addNEMiddle(array, 100000); + // for (int i = 0; i < 100000; i++) { + // array.add(array.size() / 2, i); + // } + array.addNelements(array.size() / 2, 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); - //} - addNEMiddle(list, 100000); + // for (int i = 0; i < 100000; i++) { + // list.add(list.size() / 2, i); + // } + list.addNelements(list.size() / 2, 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)); } }