Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does List.RemoveAt(index) work?

Tags:

c#

Let's say I have a list, messages, with three items. I wan't to loop through them and remove one item at a time.

for (int i = 0; i < messages.Count; i++)
{ 
    messages.RemoveAt(i);
}

(I've removed lots of irrelevant code)

What happen to the remaining messages after the first iteration? Are they moved to another index or can I do it like this to remove all three messages?

Thank you

like image 274
Mangs Avatar asked Mar 20 '14 14:03

Mangs


People also ask

What is the difference between remove and RemoveAt?

Now, the difference between Remove and RemoveAt is that one takes an item to remove from the list, and one takes an index.

How do I drop an index in a list?

You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output.

How do I remove a specific item from a list in Python?

How to Remove an Element from a List Using the remove() Method in Python. To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method. remove() will search the list to find it and remove it.

What is RemoveAt in C#?

The RemoveAt() method in C# is used to remove an element in a list at a position you set.


2 Answers

The index of all elements behind the index you remove will be decremented. If you want to avoid this with your loop let it run in reverse (delete from highest index to lowest).

for (int i = messages.Count - 1; i >= 0; i--)
{ 
    messages.RemoveAt(i);
}

or just use

messages.Clear()

to delete all elements at once without taking care about any indices.

If you just want to clear the List it's also more efficient to use Clear since it is a O(n) operation. RemoveAt is O(n) as well but inside another O(n) loop which makes it O(n^2) - not that it would matter with 3 elements as mentionend in your example but when talking about larger lists it would certainly make a difference.

like image 58
Marwie Avatar answered Oct 06 '22 17:10

Marwie


In your code, it's simpler to just call messages.Clear();. There's no need to remove each element separately.

Your code will skip every other element as it removes them until the for loop's conditional is no longer met. It will remove the elements at indexes 0 and 2 because you said your collection has three elements.

Let's step through your algorithm:

Initially, the list has three items, listed with their indexes: 0: "Hello", 1: "World", and 2: "Foo".

Your loop removes the element at index 0. The list now looks like this:

0: "World", 1: "Foo"

However, your loop executes again, since i now equals 1 and 1 < 2. The element at index 1 is then removed:

0: "World"

i is incremented to 2 and the conditional is no longer met (i is not less than 1). Your list now consists of what used to be the second element.

like image 37
NathanAldenSr Avatar answered Oct 06 '22 16:10

NathanAldenSr