标签:
深度拷贝 List<string[]> 到一个新的List<string[]> 中(tempList),
遍历tempList,移除原list中重复的string[],代码如下:
static void Main(string[] args) { List<string[]> list = new List<string[]>(); list.Add(new string[]{"1","2","3"}); list.Add(new string[] { "1","2" ,"3"}); list.Add(new string[] { "1" }); list.Add(new string[] { "1" }); list.Add(new string[] { "1" }); var tempList = new List<string[]>(list); if (list.Count >= 2) //确保下标i不越界 { for (int i = 1; i < tempList.Count; i++) { if (string.Join(",", tempList[i]) == string.Join(",", tempList[i - 1])) { list.Remove(tempList[i]); } } } foreach (var item in list) { string s = string.Join(",", item); Console.WriteLine(s); } Console.ReadKey(); }
运行截图如下:
那么问题又来了,挖掘机技术……呸! 如果是List<List<string[]>>的集合又该如何去重呢?
原理是一样的把List<string[]>变成字符串,装到List<string>中,根据List<sting>重复的元素的下标索引,删除原集合中重复的元素,
代码如下:
static void Main(string[] args) { List<string[]> list = new List<string[]>(); list.Add(new string[]{"1","2","3"}); list.Add(new string[] { "1","2" ,"3"}); list.Add(new string[] { "1" }); list.Add(new string[] { "1" }); list.Add(new string[] { "1" }); List<string[]> list2 = new List<string[]>(); list2.Add(new string[] { "1", "2", "3" }); list2.Add(new string[] { "1", "2", "3" }); list2.Add(new string[] { "1" }); list2.Add(new string[] { "1" }); list2.Add(new string[] { "1" }); List<string[]> list3 = new List<string[]>(); list3.Add(new string[] { "1", "2", "3" ,"4","5"}); list3.Add(new string[] { "1", "2", "3" }); list3.Add(new string[] { "1" }); list3.Add(new string[] { "1" }); list3.Add(new string[] { "1" }); List<List<string[]>> superList = new List<List<string[]>>(); //集合list和集合list2是相同的 superList.Add(list); superList.Add(list2); superList.Add(list3); List<string> strList = new List<string>(); foreach (var d in superList) { StringBuilder sb = new StringBuilder(); foreach (var dd in d) { string s = string.Join(",", dd); sb.Append(s); } string str = sb.ToString(); strList.Add(str); //把superList中每个子元素拼接成一条字符串放到strList中 } if (strList.Count >= 2) { for (int i = 1; i < strList.Count; i++) { if (strList[i] == strList[i - 1]) { superList.RemoveAt(i);//根据strList中重复的字符串的下标索引,删除superList中的元素 } } } Console.WriteLine(superList.Count()); Console.ReadKey(); }
运行截图如下:
标签:
原文地址:http://www.cnblogs.com/527289276qq/p/4590395.html