Progamminnoob Progamminnoob - 5 months ago 18
Java Question

removing 1 million integers from an arraylist and linkedlist one by one from the end of both lists

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");
}
}

ray ray
Answer

This is a re-write of my original answer to clarify some things in my original response and to address the question a bit more directly than I had done previously.

Your code is doing this:

for (int i = 1; i <= 1000000; i++) {
    Array1.add(i);
    Array1.remove(i);   // <<--- problem here
}

When you call Array1.add(i) you're adding the ith value into Array1. However, when you call Array1.remove(i) you're trying to remove the value at index i.

The fact that you add value i and then try to remove from index i means that you're not doing what you think you're doing.

Even in your first iteration, you end up with:

  0 
+---+
| 1 |   Array1.add(1)
+---+

  0 
+---+
| 1 |   Array1.remove(1): IndexOutOfBoundsException, only index 0 is valid
+---+

The remove operation is not trying to find the value 1 to remove it. It's trying to find whatever exists at index 1 in the array, which causes the exception.

Comments