I was working with ArrayList
and trying to add new object
at starting of ArrayList
of a prepopulated ArrayList
having 3000000
record all ready.
According to my knowledge it will add new object at first index and move all record below to their previous position. And this should happen each time when i add new object to that array list. Means execution time should be same (Little variation may happen).
But when i add new record its shows 0 and some time 15.
Here is my program
package com.rais;
import java.util.ArrayList;
import java.util.List;
public class ArrayListTest {
public static List<String> arrList = new ArrayList<String>();
static {
for (int i = 0; i < 3000000; i++) {
arrList.add("Hello"+i);
}
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
long startTime = System.currentTimeMillis();
arrList.add(0,"Rais"+i);
long endTime = System.currentTimeMillis();
System.out.println("Total execution time ="+(endTime-startTime));
}
}
}
And here is output of that program.
Total execution time =0
Total execution time =0
Total execution time =15
Total execution time =0
Total execution time =0
I am confused why it shows 0 . It should show 15 or little near to 15 each time but it should not show 0.