I was trying an exercise where I would add 1000 elements to an arraylist and then remove them systematically from the list again(by specifying the index). The idea behind this is to compare the performance of the LinkedList to the ArrayList.
int totalObjects = 0;
for(int i = 0; i < 1000; i++)
{
totalObjects += 1;
al.add("Object " + totalObjects);
}
System.out.println("The Arraylist size is " + al.size());
If i do the following only half of the elements gets removed...why is that?
for(int index = 0; index < al.size(); index++)
{
al.remove(index);
}
System.out.println("The Arraylist size after removal is " + al.size());
kind regards
Arian
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…