--- /dev/null
+import java.util.List;
+import java.util.ListIterator;
+
+public class ListExtension<E> {
+ private List<E> list;
+
+ public ListExtension() {
+ setList(null);
+ }
+
+ public ListExtension(List<E> l) {
+ setList(l);
+ }
+
+ /**
+ * [setList description]
+ * @param l [description]
+ */
+ public void setList(List<E> l) {
+ list = l;
+ }
+
+ /**
+ * [getList description]
+ * @return [description]
+ */
+ public List<E> getList() {
+ return list;
+ }
+
+ public boolean add(E e) {
+ return list.add(e);
+ }
+
+ /**
+ * Should mimic the List<E> add(int index, T value) method
+ * @param value [description]
+ * @return [description]
+ */
+ public void addMiddleIter(E value) {
+ int mid = list.size() / 2;
+ ListIterator<E> 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);
+ }
+
+ public void addNEMiddle(int Nelements) {
+ ListIterator<E> 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());
+ }
+ }
+
+}
import java.util.ArrayList;
import java.util.LinkedList;
-import java.util.ListIterator;
import java.util.concurrent.TimeUnit;
class Main {
- private static <E> void addMiddle(ArrayList<E> array, E value) {
- int mid = array.size() / 2;
- array.add(mid + 1, value);
- }
-
- private static <E> void addMiddle(LinkedList<E> 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 <E> void addMiddleIter(ArrayList<E> array, E value) {
- int mid = array.size() / 2;
- ListIterator<E> 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 <E> void addMiddleIter(LinkedList<E> list, E value) {
- int mid = list.size() / 2;
- ListIterator<E> 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 <E> void addNEMiddle(ArrayList<E> array, int Nelements) {
- ListIterator<E> 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 <E> void addNEMiddle(LinkedList<E> list, int Nelements) {
- ListIterator<E> 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<Integer> array = new ArrayList<Integer>();
- LinkedList<Integer> list = new LinkedList<Integer>();
+ ListExtension<Integer> array = new ListExtension<Integer>(new ArrayList<Integer>());
+ ListExtension<Integer> list = new ListExtension<Integer>(new LinkedList<Integer>());
for (int i = 0; i < 1000000; i++) {
array.add(i);
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));
}
}
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Scanner;
+import java.io.IOException;
class Main {
}
- /**
- * The main() function
- * @param String[] args main() function arguments array
- */
- public static void main(String[] args) {
+ private static void main_A(String[] args) {
if (args.length == 0) {
System.out.println("Please run with java Main <class name to inspect>");
System.exit(-1);
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i]);
}
+ }
+
+ /**
+ * The main() function
+ * @param String[] args main() function arguments array
+ */
+ public static void main(String[] args) {
+ String name = Entiers.class.getName();
+ Class cl = null;
+ Object o = null;
+ try {
+ cl = Class.forName(name);
+ Class[] types = new Class[]{Integer.class};
+ Constructor ct = cl.getConstructor(types);
+ o = ct.newInstance(new Integer(100));
+ }
+ catch (ClassNotFoundException e) {
+ System.out.println("Class name do not exist.");
+ System.exit(-1);
+ }
+ catch (NoSuchMethodException e) {
+ e.printStackTrace();
+ }
+ catch (InstantiationException e) {
+ e.printStackTrace();
+ }
+ catch (IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ catch (InvocationTargetException e) {
+ e.printStackTrace();
+ }
+ Scanner uInput = null;
+ try {
+ uInput = new Scanner(System.in);
+ System.out.println("Saisir le nom de la methode à invoquer:");
+ String method = uInput.next();
+ //TODO: one can build the input list from the method arguments list and types
+ System.out.println("Saisir l'argument entier paramètre de la méthode:");
+ int integer = uInput.nextInt();
+ Method m = cl.getMethod(method, new Class[]{Integer.class});
+ m.invoke(o, integer);
+ }
+ catch (Exception e) {
+ System.out.println("Erreur:");
+ e.printStackTrace();
+ }
+ finally {
+ try {
+ Method mDisplay = cl.getMethod("afficher");
+ mDisplay.invoke(o);
+ }
+ catch (NoSuchMethodException e) {
+ e.printStackTrace();
+ }
+ catch (IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ catch (InvocationTargetException e) {
+ e.printStackTrace();
+ }
+ uInput.close();
+ }
}
}