[英]How to destroy elements (or shorten length) of list array in C#
I have the following list: 我有以下清单:
List<MyOwnList> mylist = new List<MyOwnList>();
mylist[0].Name = "Name0";
mylist[0].Class = "Class0";
mylist[1].Name = "Name1";
mylist[1].Class = "Class1";
mylist[2].Name = "Name2";
mylist[2].Class = "Class2";
mylist[3].Name = "Name3";
mylist[3].Class = "Class3";
mylist[4].Name = "Name4";
mylist[4].Class = "Class4";
I want to shorten the length or let's say destroy elements from position 3 and 4. I used the following code but it still prints "5" when I do mylist.Count
我想缩短长度,或者说销毁位置3和4的元素。我使用了以下代码,但是在执行
mylist.Count
时,它仍然显示“ 5”。
for(int i=0; i<mylist.Count; i++)
{
if(i>2)
{
mylist[i] = null;
}
}
I expect it to print "3" when I do mylist.Count
我希望在执行
mylist.Count
时它会打印“ 3”。
When you do this: 执行此操作时:
mylist[i] = null;
you're accually setting i
th element to null, so you won't change size of your list. 您将第
i
个元素设置为null,因此您不会更改列表的大小。 Basicaly you will have null
there: 基本上,在那里您将有
null
:
// true
bool elementIsNull = mylist[i] == null;
Use RemoveRange
method: 使用
RemoveRange
方法:
// remove 2 elements starting at element with index 3
mylist.RemoveRange(3, 2);
it's depend how you define "destroy" 这取决于您如何定义“破坏”
if you want to remove the element from the list and "destroy" the cell to reduce the list list you can use the RemoveAt
- BUT it would shorten the list 如果要从列表中删除元素并“销毁”单元格以缩小列表,则可以使用
RemoveAt
但它会缩短列表
if you want to "destroy" the element , GC will take care of that , as long no one is holding reference to this element 如果您要“销毁”该元素,则只要没有人持有对该元素的引用,GC会予以解决。
It really depends what you mean by Destroy
. 这确实取决于您对
Destroy
理解。
If MyOwnList
implements IDisposable
, You would use: 如果
MyOwnList
实现IDisposable
,则可以使用:
int startIndex;
int numberOfItemsToRemove;
mylist.GetRange(startIndex, numberOfItemsToRemove).ForEach(m => m.Dispose());
mylist.RemoveRange(startIndex, numberOfItemsToRemove);
Otherwise: 除此以外:
int startIndex;
int numberOfItemsToRemove;
mylist.RemoveRange(startIndex, numberOfItemsToRemove);
You have to delete the items in reverse to avoid the indexoutofrange exception and please don't use a property of a list which is going to change in the condition of a loop 您必须反向删除项目以避免indexoutofrange异常,并且请不要使用将在循环条件下更改的列表属性
try this: 尝试这个:
List<MyOwnList> mylist = new List<MyOwnList>();
mylist.Add(new MyOwnList { Name = "Name0", Class = "Class0" });
mylist.Add(new MyOwnList { Name = "Name1", Class = "Class1" });
mylist.Add(new MyOwnList { Name = "Name2", Class = "Class2" });
mylist.Add(new MyOwnList { Name = "Name3", Class = "Class3" });
mylist.Add(new MyOwnList { Name = "Name4", Class = "Class4" });
mylist[0].Name = "Name0";
mylist[0].Class = "Class0";
mylist[1].Name = "Name1";
mylist[1].Class = "Class1";
mylist[2].Name = "Name2";
mylist[2].Class = "Class2";
mylist[3].Name = "Name3";
mylist[3].Class = "Class3";
mylist[4].Name = "Name4";
mylist[4].Class = "Class4";
int count = mylist.Count;
for (int i = count - 1; i >= 0; i--)
{
if (i > 2)
{
mylist.RemoveAt(i);
//mylist[i] = null;
}
}
Console.WriteLine(mylist.Count);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.