I am trying to prove that adding 1 million integers to an arrayList and then deleting them 1 by 1 from the end vs doing the same thing with a linkedlist is faster or slower in milliseconds and seconds.. having issues with removing from the end of the arraylist and removing from the end of linkedlist. it gives me an indexoutofboundsexception. here is my code so far:
package blah;
import java.util.ArrayList;
import java.util.LinkedList;
public abstract class blah {
public static void main(String[] args) {
ArrayList<Integer> Array = new ArrayList<Integer>();
LinkedList<Integer> Link = new LinkedList<Integer>();
ArrayList<Integer> Array1 = new ArrayList<Integer>();
LinkedList<Integer> Link1 = new LinkedList<Integer>();
long start;
long stop;
long result;
start = System.currentTimeMillis();
for (int i = 1; i <= 1000000; i++) {
Array.add(0, i);
}
stop = System.currentTimeMillis();
result = stop - start;
System.out.println("ArrayList time : " + result + " milliseconds");
System.out.println("ArrayList time: " + result / 1000 + " seconds");
System.out.println("");
start = System.currentTimeMillis();
for (int i = 1; i <= 1000000; i++) {
Array1.add(i);
Array1.remove(i);
}
stop = System.currentTimeMillis();
result = stop - start;
System.out.println("ArrayList time : " + result + " milliseconds");
System.out.println("ArrayList time : " + result / 1000 + " seconds");
System.out.println("");
start = System.currentTimeMillis();
for (int i = 1; i <= 1000000; i++) {
Link.add(0, i);
}
stop = System.currentTimeMillis();
result = stop - start;
System.out.println("LinkedList time : " + result + " milliseconds");
System.out.println("LinkedList time : " + result / 1000 + " seconds");
System.out.println("");
start = System.currentTimeMillis();
for (int i = 1; i <= 1000000; i++) {
Link1.add(i);
Link1.remove(i);
}
stop = System.currentTimeMillis();
result = stop - start;
System.out.println("LinkedList time : " + result + " milliseconds");
System.out.println("LinkedList time : " + result / 1000 + " seconds");
}
}
Aucun commentaire:
Enregistrer un commentaire