简体   繁体   中英

How to remove the element from List in C#?

I got a liiitle problem.

There is

List<List<UInt32>> temp = new List<List<UInt32>>();

For example,

there are two List<UInt32> records within the List temp however, when i try to do something like temp.removeAt(0); it doesn't remove the first row (List<UInt32> ) .. Why is that? Do i do something wrong?

Update

Here is the code that doesn't work. I know there are 2 List<UInt32> within the main List and I know that the first List<UInt32>.Count is 1 but when I check the same position after this code, the first code's Count is still one.

                        int i = 0;
                        bool boolA = true;

                        while (boolA)
                        {
                            if (temp[i].Count == 1) 
                            {
                                temp.RemoveAt(i);
                                temps++; 
                            }

                            if (i == temp.Count - 1) boolA = false;
                            i++;

You are removing items, then advancing i, therefore skipping the item that moved into the previous position that i represented.

eg

items = 5 i = 0 -> remove i => items = 4, i = 0

-> i++ => items = 4, i = 1

-> remove i => items= 3, i = 1

-> i++ => items = 3, i = 2

-> remove i => items = 3, i = 2

-> i++ => items = 3, i = 3

-> remove i => index out of range

Your question is unclear, but it sounds like your user interface is bound to the list. If so, you won't see changes because the list doesn't support change notification. You should use a BindingList<T> (for windows forms) or ObservableCollection<T> (for WPF) to get change notification.

This does work.

If there are two List instances in your "temp" list, doing temp.RemoveAt(0); will remove the first of the two lists. It will remove the entire List from temp, not just the first int value in the internal list.

I believe it does remove it.

I think you are looking at the wrong list when checking the count, since you're dealing with a list of lists of integers...

I think we will need more of your code to figure out the issue. What your describing should work.

I had the following code at it worked correctly.

        List<UInt32> a = new List<UInt32>();
        a.Add(1);
        a.Add(11);

        List<UInt32> b = new List<UInt32>();
        b.Add(2);
        b.Add(22);

        List<UInt32> c = new List<UInt32>();
        c.Add(3);
        c.Add(33);

        List<List<UInt32>> temp = new List<List<UInt32>>();
        temp.Add(a);
        temp.Add(b);
        temp.Add(c);

        temp.RemoveAt(0);

On the last line, the list that had contained "1" and "11" was removed.

It's important to note that the entire first list was removed and not just the item that contained the number "1".

We may need to see more of your code. One thing that stands out is the line in your example:

if (temp[i].Count == 1)

This condition will only be met if the inner List<UInt32> only has 1 item in it. You may not be executing the .RemoveAt(0) like you think you are. I can't say for sure without seeing more of the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM