List<string[]> 如何去重,代码如下:

static void Main(string[] args)
{ List<string[]> list = new List<string[]>(); list.Add(new string[] { "", "", "" });
list.Add(new string[] { "" });
list.Add(new string[] { "", "", "" });
list.Add(new string[] { "" });
list.Add(new string[] { "" }); 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 >= ) //确保下标i不越界
{
string currentStr = string.Empty;
for (int i = ; i < strList.Count; i++)
{
currentStr = strList[i];
for (int j = i + ; 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(); }
}

运行截图如下:

那么问题又来了,挖掘机技术……呸! 如果是List<List<string[]>>的集合又该如何去重呢?

原理是一样的把List<string[]>变成字符串,装到List<string>中,根据List<sting>重复的元素的下标索引,删除原集合中重复的元素,

代码如下:

 static void Main(string[] args)
{
List<string[]> list = new List<string[]>(); list.Add(new string[]{"","",""});
list.Add(new string[] { "","" ,""});
list.Add(new string[] { "" });
list.Add(new string[] { "" });
list.Add(new string[] { "" }); List<string[]> list2 = new List<string[]>(); list2.Add(new string[] { "", "", "", "", "" });
list2.Add(new string[] { "", "", "" });
list2.Add(new string[] { "" });
list2.Add(new string[] { "" });
list2.Add(new string[] { "" }); List<string[]> list3 = new List<string[]>();
list3.Add(new string[] { "", "", "" });
list3.Add(new string[] { "", "", "" });
list3.Add(new string[] { "" });
list3.Add(new string[] { "" });
list3.Add(new string[] { "" }); List<string[]> list4= new List<string[]>(); list4.Add(new string[] { "", "", "", "", "" });
list4.Add(new string[] { "", "", "" });
list4.Add(new string[] { "" });
list4.Add(new string[] { "" });
list4.Add(new string[] { "" });
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个以上的元素才有可能出现重复
{
string currentStr = string.Empty;
for (int i =; i < strList.Count; i++)
{
currentStr = strList[i];
for (int j =i+; 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();
}

运行截图如下:

List<string[]> 如何去重的更多相关文章

  1. String中的intern方法

    上一篇你真的会用String吗(3)-关于字符串拼接中我们提到了String.intern()方法,本篇我们就来详细的看下这个方法是干嘛的.首先来看下jdk8中这个方法的注释: When the in ...

  2. JVM系列之:String.intern和stringTable

    目录 简介 intern简介 intern和字符串字面量常量 分析intern返回的String对象 分析实际的问题 G1中的去重功能 总结 简介 StringTable是什么?它和String.in ...

  3. Stream流的基本介绍以及在工作中的常用操作(去重、排序以及数学运算等)

    平时工作中,我在处理集合的时候,总是会用到各种流操作,但是往往在处理一些较为复杂的集合时,还是会出现无法灵活运用api的场景,这篇文章的目的,主要是为介绍一些工作中使用流时的常用操作,例如去重.排序和 ...

  4. 2019-04-28 Mybatis generator逆向工程生成的Example代码分析

    今天主要对Mybatis generator生成的DAO层等进行分析,讲解Example类的使用和扩展 1.先在数据库建表 CREATE TABLE `department` ( `fid` ) NO ...

  5. SpringBoot系列——Spring-Data-JPA

    前言 jpa是ORM映射框架,更多详情,请戳:apring-data-jpa官网:http://spring.io/projects/spring-data-jpa,以及一篇优秀的博客:https:/ ...

  6. java 11 增加了一系列的字符串处理方法,Optional 加强 ,改进的文件API

    增加了一系列的字符串处理方法 如以下所示. // 判断字符串是否为空白 " ".isBlank(); // true // 去除首尾空白 " Javastack &quo ...

  7. excel合并

    import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; impor ...

  8. C# - LINQ 语言集成查询

    LINQ(Language Integrated Query) LINQ语言集成查询是一组用于C#语言的扩展.它允许编写C#代码对数据集进行查询,比如查询内存中的对象或查询远程数据库的表.利用linq ...

  9. JVM参数简述

    java虚拟机启动时会带有很多的启动参数,Java命令本身就是一个多参数的启动命令.那么具体JVM启动包含哪些参数呢?这篇文章针对java8的情况做一篇汇总解读,包含大多数常见和不常见的命令参数,过于 ...

随机推荐

  1. Java中的Math类的简单实用

    System.out.println(Math.PI);//获取PI的值 System.out.println(Math.E);//常量E int min = Math.min(5, 4);//求最小 ...

  2. Google科学家前腾讯副总裁吴军将出席第二届万物互联创新大会

    当越来越多的科技产品注入互联网的基因,"万物互联"的模式悄然兴起.第二届万物互联创新大会(B12大会)将于2016-11-13日在杭州市余杭区隆重召开.Google科学家前腾讯副总 ...

  3. Spring Security(09)——Filter

    目录 1.1     Filter顺序 1.2     添加Filter到FilterChain 1.3     DelegatingFilterProxy 1.4     FilterChainPr ...

  4. 推荐一篇很好的介绍wpf dependency property的文章

    http://www.codeproject.com/Articles/140620/WPF-Tutorial-Dependency-Property

  5. 浙大pat 1025题解

    1025. PAT Ranking (25) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...

  6. HttpModule的基本概念

    注:本文为个人学习摘录,原文地址:http://www.cnblogs.com/stwyhm/archive/2006/08/09/471765.html HttpModule是如何工作的 当一个HT ...

  7. Leetcode016 3Sum Closest

    public class S016 { //借鉴S015的思想,只是稍微有点慢 public int threeSumClosest(int[] nums, int target) { Arrays. ...

  8. [ An Ac a Day ^_^ ] CodeForces 426C Sereja and Swaps 优先队列

    题意: 给你一个有n个数的序列 取一个区间 这个区间内的数可以与区间外的值交换k次 问这样的区间最大值是多少 思路: 看数据是200 时间复杂度O(n*n) 应该可以暴力 顺便学习一下优先队列 枚举区 ...

  9. angularjs表单

    <form ng-app="myApp" ng-controller="validateCtrl" name="myForm" nov ...

  10. Python笔记3-20151027

    函数的参数 Python的函数定义非常简单,但是灵活度却非常大.除了正常定义的必选参数外,还可以使用默认参数.可变参数和关键字参数,使得函数定义出来的接口,不但能处理复杂的参数,还可以简化调用者的代码 ...