如何从C#中的List中删除元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/730575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:55:47  来源:igfitidea点击:

How to remove the element from List in C#?

c#genericslist

提问by Skuta

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?

List<UInt32>然而,List temp 中有两条记录,当我尝试执行类似操作时,temp.removeAt(0);它不会删除第一行(List<UInt32>) .. 为什么会这样?我做错了什么吗?

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>.Countis 1 but when I check the same position after this code, the first code's Count is still one.

这是不起作用的代码。我知道List<UInt32>主列表中有 2 个,我知道第一个List<UInt32>.Count是 1,但是当我在这段代码之后检查相同的位置时,第一个代码的计数仍然是一个。

                        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++;

采纳答案by cjk

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

您正在删除项目,然后推进 i,因此跳过移动到 i 表示的先前位置的项目。

e.g.

例如

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

items = 5 i = 0 -> 删除 i => items = 4, i = 0

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

-> i++ => 项目 = 4,i = 1

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

-> 删除 i => items= 3, i = 1

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

-> i++ => 项目 = 3,i = 2

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

-> 删除 i => 项目 = 3,i = 2

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

-> i++ => 项目 = 3,i = 3

-> remove i => index out of range

-> 删除 i => 索引超出范围

回答by Lucero

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...

我认为您在检查计数时正在查看错误的列表,因为您正在处理整数列表的列表......

回答by Kent Boogaart

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.

您的问题不清楚,但听起来您的用户界面已绑定到列表中。如果是这样,您将看不到更改,因为该列表不支持更改通知。您应该使用BindingList<T>(对于 Windows 窗体)或ObservableCollection<T>(对于 WPF)来获取更改通知。

回答by Reed Copsey

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.

如果您的“临时”列表中有两个 List 实例,执行temp.RemoveAt(0);将删除两个列表中的第一个。它将从 temp 中删除整个 List,而不仅仅是内部列表中的第一个 int 值。

回答by epotter

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.

在最后一行,删除了包含“1”和“11”的列表。

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

需要注意的是,整个第一个列表已被删除,而不仅仅是包含数字“1”的项目。

回答by Alexander Kahoun

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.

仅当内部List<UInt32>仅包含 1 个项目时,才会满足此条件。您可能没有.RemoveAt(0)像您认为的那样执行。如果没有看到更多代码,我不能肯定地说。