找到你要的答案

Q:how to remove a number from a “List”?

Q:如何从“列表”中删除数字?

I'm programming in Visual studio 2013 C#.

If listname.add is adding a number, so how to remove a number from a list?

我在Visual Studio 2013 C #编程。

如果listname.add是增加一个数,所以如何从列表中删除一个号码吗?

answer1: 回答1:

The complement of "add" is not "subtract" for a list, it's "remove".

To remove an item with a specific value you can use the Remove method:

listname.Remove(value);

To remove an item at a specific index you can use the RemoveAt method:

listname.RemoveAt(index);

To remove multiple values you can use the RemoveAll method, which will remove all items that match a condition:

listname.RemoveAll(i => i == value);

Note that the Remove method will search through the list until it finds the value. If this is something that you do frequently, you might want to use differnt collection, like a HashSet<int> where removing a value is an O(1) operation instead of an O(n) operation.

“添加”的补充不是“减去”的列表,它的“删除”。

若要移除具有特定值的项,可以使用移除方法:

listname.Remove(value);

删除在一个特定的索引可以使用RemoveAt方法项目:

listname.RemoveAt(index);

将多个值可以使用RemoveAll方法,这将删除符合条件的所有项目:

listname.RemoveAll(i => i == value);

注意,移除方法将在列表中搜索,直到找到该值为止。如果这是你经常做的,你可能想要使用不同的集合,就像一个好& lt;int & gt;删除值是O(1)操作,而不是一个O(n)的操作。

answer2: 回答2:

thanks you all guys. Guffa solved it. you just need to enter "listname.Remove(value);" its my first question here. and I'm going to come back. for those who wondering- I'm developing A game. I I needed a list to set my levels system, thanks for your help!

谢谢你们大家。噶发解决它。你只需要输入“listName。删除(价值);“我的第一个问题在这里。我要回来。对于那些想知道-我正在开发一个游戏。我需要一个列表来设置我的等级系统,谢谢你的帮助!

c#  visual-studio-2013