Liststring[] 如何去重
生活随笔
收集整理的這篇文章主要介紹了
Liststring[] 如何去重
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
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" });list.Add(new string[] { "1", "2", "3" });list.Add(new string[] { "1" });list.Add(new string[] { "1" });List<string> strList = new List<string>();foreach (var item in list){string s = string.Join(",", item);strList.Add(s);}//要刪除的元素的下標集合List<int> removeIndexList = new List<int>();if (list.Count >= 2) //確保下標i不越界 {string currentStr = string.Empty;for (int i = 0; i < strList.Count; i++){currentStr = strList[i];for (int j = i + 1; j < strList.Count; j++){if (currentStr == strList[j]){//添加到要刪除的索引集合removeIndexList中 removeIndexList.Add(j);}}}removeIndexList = removeIndexList.Distinct().ToList();////去除重復的索引//添加到要刪除的對象集合List<string[]> removeList = new List<string[]>();foreach (var index in removeIndexList){removeList.Add(list[index]);}//遍歷要刪除對象的集合,刪除原集合中的對象foreach (var item in removeList){list.Remove(item);}foreach (var item in list){string s = string.Join(",", item);Console.WriteLine(s);}Console.ReadKey();}} View Code?
運行截圖如下:
?
?
那么問題又來了,挖掘機技術……呸! 如果是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", "4", "5" });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" });list3.Add(new string[] { "1", "2", "3" });list3.Add(new string[] { "1" });list3.Add(new string[] { "1" });list3.Add(new string[] { "1" });List<string[]> list4= new List<string[]>();list4.Add(new string[] { "1", "2", "3", "4", "5" });list4.Add(new string[] { "1", "2", "3" });list4.Add(new string[] { "1" });list4.Add(new string[] { "1" });list4.Add(new string[] { "1" });List<List<string[]>> superList = new List<List<string[]>>();//集合list和集合list3是相同,list2和list4相同,并且list4添加了2次 superList.Add(list);superList.Add(list2);superList.Add(list3);superList.Add(list4);superList.Add(list4);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中 }//要刪除的元素的下標集合List<int> removeIndexList = new List<int>();if (strList.Count >= 2) //有2個以上的元素才有可能出現重復 {string currentStr = string.Empty;for (int i =0; i < strList.Count; i++){currentStr = strList[i];for (int j =i+1; j < strList.Count; j++){if (currentStr == strList[j]){//添加到要刪除的索引集合removeIndexList中 removeIndexList.Add(j);}}}}removeIndexList = removeIndexList.Distinct().ToList();//去除重復的索引//要刪除的對象集合List<List<string[]>> superRemoveList = new List<List<string[]>>();foreach (var index in removeIndexList){superRemoveList.Add(superList[index]);}foreach (var item in superRemoveList){superList.Remove(item);}Console.WriteLine(superList.Count());Console.ReadKey();} View Code?
?
運行截圖如下:
?
轉載于:https://www.cnblogs.com/527289276qq/p/4590395.html
總結
以上是生活随笔為你收集整理的Liststring[] 如何去重的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10个利用Eclipse调试Java的常
- 下一篇: 强制修改mysql 中root的密码