您的位置:首页 > 编程语言 > C#

c#删除list中的元素

2013-09-29 15:02 288 查看
publicstaticvoidTestRemove(){
string[]str={"1","2","d","x"};
List<string>list=newList<string>(str);

#regionhaserror
for(inti=0;i<list.Count;i++)
{
Console.WriteLine(list[i]);
Console.WriteLine("Result:"+i+":"+i/2);
if(i%2==0)
{
list.Remove(list[i]);
}
}
#endregion

Console.WriteLine("============================================");
list=newList<string>(str);
for(inti=list.Count-1;i>=0;i--)
{
Console.WriteLine(list[i]);
Console.WriteLine("Result:"+i+":"+i/2);
if(i%2==0)
{
Console.WriteLine(string.Format("delete.index:{0}value:{1}",i,list[i]));
list.Remove(list[i]);

}
}
}
output:

1
Result:0:0
d
Result:1:0
x
Result:2:1
============================================
x
Result:3:1
d
Result:2:1
delete.index:2value:d
2
Result:1:0
1
Result:0:0
delete.index:0value:1


.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: