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. git安装后配置--config

    安装git后需要配置一下环境,每台计算机上只需要配置一次,程序升级时会保留配置信息. 你可以在任何时候再次通过运行命令来修改它们. 通过git config命令来配置环境变量,这些变量存储在三个不同的 ...

  2. JMS理解2

    使用JMS 的应用程序被称为JMS 客户端,处理消息路由与传递的消息系统被称为JMS Provider,而JMS 应用则是由多个JMS 客户端和一个JMS Provider 构成的业务系统.发送消息的 ...

  3. POJ 1323 Game Prediction#贪心

    (- ̄▽ ̄)-* //既然是求最少能胜几次 //说明对方是要尽可能让我输 //但为了避免浪费,对方会用比我的牌大的牌中的最小pip的牌来击败我 #include<iostream> #in ...

  4. C# Monads的实现(一)

    了解Haskell语言的朋友都知道它是一门纯函数式编程,输出结果只跟输入参数相关,这导致Haskell不能有输入输出函数,因为不同的环境下,输入相同,但输出可能有所不同.Haskell语言中,变量的值 ...

  5. asp.net正则表达式去除a标签

    if (drr["allow_a"].ToString() == "False") { cont = dr["news_Content"]. ...

  6. 一步一步学EF系列【4、升级篇 实体与数据库的映射】live writer真坑,第4次补发

    前言 之前的几篇文章,被推荐到首页后,又被博客园下了,原因内容太少,那我要写多点呢,还是就按照这种频率进行写呢?本身我的意图这个系列就是想已最简单最容易理解的方式进行,每篇内容也不要太多,这样初学者容 ...

  7. 关闭HTC手机充电时屏幕一直亮着绿色电池的办法

            首先必须取得ROOT权限,用RE文件管理器进system/bin/把最下面的zchgd文件改名字或者直接删掉重新启动手机充电时就不会亮屏了,电池图标也不会出来,再用数据线连电脑充电吧, ...

  8. erlang dets

    1.dets表包含set.bag.和duplicate bag 2.dets:open_file(TableName,Options)创建或打开表 3.Options 1){auto_save,Int ...

  9. Weblogic的集群

    <收藏自http://www.cnblogs.com/HondaHsu/p/4267972.html> 一.Weblogic的集群 还记得我们在第五天教程中讲到的关于Tomcat的集群吗? ...

  10. Sql Sever语句 (续2)

    日期时间 把日期类型的字段下默认值或绑定里的 设置成getdate()   ,新建字段时候不设置会自动获取当前服务器时间 在当前时间加上xx年xx月xx天 select datedd(yy,100,g ...