c#删除list中的元素
public static void TestRemove() {
string[] str = { "1", "2", "d", "x" };
List<string> list = new List<string>(str);
#region has error
for (int i = 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 = new List<string>(str);
for (int i = 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:2 value:d
2
Result:1:0
1
Result:0:0
delete .index:0 value:1
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { 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; }
c#删除list中的元素的更多相关文章
- Java 删除ArrayList中重复元素,保持顺序
// 删除ArrayList中重复元素,保持顺序 public static List<Map<String, Object>> removeDuplicat ...
- Java中如何优雅地删除List中的元素
在工作中的许多场景下,我们都会使用到List这个数据结构,那么同样的有很多场景下需要删除List中的某一个元素或某几个元素,那么我们该如何正确无误地删除List中的元素的,今天我来教大家三种方式. 前 ...
- 正确遍历删除List中的元素方法(推荐)
遍历删除List中的元素有很多种方法,当运用不当的时候就会产生问题.下面主要看看以下几种遍历删除List中元素的形式: 1.通过增强的for循环删除符合条件的多个元素 2.通过增强的for循环删除符合 ...
- 如何正确遍历删除List中的元素
遍历删除List中的元素有很多种方法,当运用不当的时候就会产生问题.下面主要看看以下几种遍历删除List中元素的形式: 1.通过增强的for循环删除符合条件的多个元素 2.通过增强的for循环删除符合 ...
- 如何正确遍历删除List中的元素,你会吗?
遍历删除List中的元素有很多种方法,当运用不当的时候就会产生问题.下面主要看看以下几种遍历删除List中元素的形式: 1.通过增强的for循环删除符合条件的多个元素 2.通过增强的for循环删除符合 ...
- LintCode之删除链表中的元素
题目描述 我的代码 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode n ...
- Python笔记:用for循环删除列表中的元素
for运行过程中会有一个指针来记录当前循环的元素是哪一个,一开始这个指针指向第0个元素,然后获取它,接着删除第0个元素,这时候,原来是第1个的元素会变成第0个,当指针向后移动一次,指向了现在第1个元素 ...
- LintCode #452 删除链表中的元素
方法很笨拙,被链表给绕住了,抽空在整理一下. /** * Definition for ListNode * public class ListNode { * int val; * ListNode ...
- 203 Remove Linked List Elements 删除链表中的元素
删除链表中等于给定值 val 的所有元素.示例给定: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6返回: 1 --& ...
- 如何正确遍历删除List中的元素(普通for循环、增强for循环、迭代器iterator、removeIf+方法引用)
遍历删除List中符合条件的元素主要有以下几种方法: 普通for循环 增强for循环 foreach 迭代器iterator removeIf 和 方法引用 其中使用普通for循环容易造成遗漏元素的问 ...
随机推荐
- C#Encoding
1.Encoding (1).如何生成一个Encoding即一种编码 Encoding位于System.Text命名空间下,是一个抽象类,它的派生类如下图: 要实例化一个Encoding一共有以下两种 ...
- 读取本地json文件,转出为指定格式json
引用添加Json.Net 引用命名空间 using Newtonsoft.Json //读取自定目录下的json文件 StreamReader sr = new StreamReader(@" ...
- MVC中用jQuery加BootStrap实现动态增加删除文本输入框!
http://www.freejs.net/article_biaodan_278.html 这是在网上找到方法,我修改了一下实合我的项目,发博只为收藏记录并加深记忆. 修改后效果如下 @model ...
- jdk-7u40-windows-i586的安装
1.预备知识: i586 指的是windows 32bit版本 Oracle.微软.IBM这些大佬们最“贵族”了-----他们都很喜欢 C盘 2.关键 JDK必须装在C盘目录下,才能在命令行下正确运行 ...
- TestNG参数化之@Parameters传参
通过TestNG实现参数话常用两种方式,一种是借助 @Parameters读取testng.xml中参数,一种是使用@DataProvider注解传参. 此次主要讲解XML传参,语法:在java类中定 ...
- vuejs 添加事件时出现TypeError: n.apply is not a function
vuejs项目中给表单元素添加事件时出现了TypeError: n.apply is not a function的错误,后来发现错误原因时处理事件的函数名和data中定义的变量名相同 当给事件添加处 ...
- H - Birthday Paradox (生日悖论)
点击打开链接 Sometimes some mathematical results are hard to believe. One of the common problems is the bi ...
- D - How Many Tables (并查集)(水题)
点击打开链接 Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius want ...
- Mac下安装HomeBrew
本文章为菲利克斯猫(Felix.mao)原创,转载务必在明显处注明:原文链接:http://blog.csdn.net/maojudong/article/details/7918291 1. 前言 ...
- [JS] 瀑布流加载
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...