Looping and arraialist and deleting elements at a specified index

I tried to do an exercise in which I would add 1000 elements to the arraylist and then remove them from the list again (by specifying an index). The idea is to compare LinkedList performance with 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 are deleted ... why is this?

 for(int index = 0; index < al.size(); index++) { al.remove(index); } System.out.println("The Arraylist size after removal is " + al.size()); 

Regards Arian

+4
source share
6 answers

This is because you change indexes by deleting. If you delete element 0, then element 1 will now become element 0. Now, when you delete 1, this is what used to be element 2, and what was element 1 still exists at index 0.

The easiest way to avoid this is to go backward from the end to the beginning.

you can simply remove index 0 until the ArrayList is empty.

+11
source

Remember that you can immediately delete all elements simply by using the clear() method. In your code, the problem is that the list changes at the same time that you iterate over it, effectively reducing its size, so the index < al.size() condition fails. Try instead:

 for (int index = 0, n = al.size(); index < n; index++) al.remove(0); 

Alternatively, this solution removes the elements at the end, which makes it more efficient (now there is no need to copy the elements):

 for (int idx = al.size() - 1; idx >= 0; idx--) al.remove(idx); 
+8
source

As you remove items from an ArrayList , its index is updated. Thus, you delete the element at position 0, and the element at position 1 is now at the position of index 0. Therefore, when you delete the element at index 1, you delete the rally element at index 2 of the original ArrayList , etc.

+2
source

Check out the loop where you delete items again. Every time you delete an element, the value returned by al.size () decreases, and the index increases. This means that you will only carry out half the time when you want.

The correction will be performed.

 int size = al.size(); for(int index = 0; index < size; index++ ) { 

then work. thus the size does not change.

Another thing to keep in mind is that when you delete something from an arraylist with index 0, index 1 will become index 0. So it would be better to iterate with

 int index = al.size(); index >=0 ; index-- 
+2
source

When you delete a list item, it is compressed.

Say you have items 0 1 and 2

Your index is 0

You delete 0, your index is now 1, you delete 2, because now in index 1.

Has the meaning?

+1
source

it's ok that only half of the list is empty, because when you remove items from the list, the size of the list decreases, but the index increases. And they meet in the middle. If you really want to delete items one by one and remove from the last to the first item. I suggest you use:

 while (!al.isEmpty()) { al.remove(al.indexOf(al.size()-1)); } 
+1
source

Source: https://habr.com/ru/post/987102/


All Articles