List<string[]> 如何去重
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[]> 如何去重的更多相关文章
- String中的intern方法
上一篇你真的会用String吗(3)-关于字符串拼接中我们提到了String.intern()方法,本篇我们就来详细的看下这个方法是干嘛的.首先来看下jdk8中这个方法的注释: When the in ...
- JVM系列之:String.intern和stringTable
目录 简介 intern简介 intern和字符串字面量常量 分析intern返回的String对象 分析实际的问题 G1中的去重功能 总结 简介 StringTable是什么?它和String.in ...
- Stream流的基本介绍以及在工作中的常用操作(去重、排序以及数学运算等)
平时工作中,我在处理集合的时候,总是会用到各种流操作,但是往往在处理一些较为复杂的集合时,还是会出现无法灵活运用api的场景,这篇文章的目的,主要是为介绍一些工作中使用流时的常用操作,例如去重.排序和 ...
- 2019-04-28 Mybatis generator逆向工程生成的Example代码分析
今天主要对Mybatis generator生成的DAO层等进行分析,讲解Example类的使用和扩展 1.先在数据库建表 CREATE TABLE `department` ( `fid` ) NO ...
- SpringBoot系列——Spring-Data-JPA
前言 jpa是ORM映射框架,更多详情,请戳:apring-data-jpa官网:http://spring.io/projects/spring-data-jpa,以及一篇优秀的博客:https:/ ...
- java 11 增加了一系列的字符串处理方法,Optional 加强 ,改进的文件API
增加了一系列的字符串处理方法 如以下所示. // 判断字符串是否为空白 " ".isBlank(); // true // 去除首尾空白 " Javastack &quo ...
- excel合并
import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; impor ...
- C# - LINQ 语言集成查询
LINQ(Language Integrated Query) LINQ语言集成查询是一组用于C#语言的扩展.它允许编写C#代码对数据集进行查询,比如查询内存中的对象或查询远程数据库的表.利用linq ...
- JVM参数简述
java虚拟机启动时会带有很多的启动参数,Java命令本身就是一个多参数的启动命令.那么具体JVM启动包含哪些参数呢?这篇文章针对java8的情况做一篇汇总解读,包含大多数常见和不常见的命令参数,过于 ...
随机推荐
- 常用JavaScript字符串方法简述
网址来源:http://www.html-js.com/article/JS-rookie-in-the-rookie-to-start-learning-to-fly-the-commonly-us ...
- javascript生成新标签的三种方法
javascript生成新标签的三种方法:http://www.cnblogs.com/online-link/p/6062423.html
- jQuery复习:第二章&第三章
第二章 一.选择器 1.层次选择器 $(“ancestor descendant”)选取ancestor元素里的所有后代元素 $(“parent > child”)选取parent元素下的chi ...
- HTML <base> 标签的 href 属性
为页面上所有相对 URL 规定基准 URL: <head> <base href="http://www.w3school.com.cn/i/" /> &l ...
- HDU 2461 Rectangles#容斥原理
http://acm.hdu.edu.cn/showproblem.php?pid=2461 题目很简单,但是由于询问数M可以很大,所以容易超时,这道题学到了在结构体里面写函数的方法,这样子效率更高, ...
- hdu_4742_Pinball Game 3D(cdq分治+树状数组)
题目链接:hdu_4742_Pinball Game 3D 题意: 给你n个点,让你求三维的LIS,并且求出有多少种组合能达到LIS. 题解: 求三维的LIS,典型的三维偏序问题,x排序,解决一维,c ...
- NSCondition
一.NSCondition是对条件变量和互斥量的一个封装,用于线程之间的同步. 其中的互斥量用于保护对条件变量的修改,条件变量变化以信号量的方式通知其它线程实现线程之间的同步. 二.NSConditi ...
- c++的复制构造函数
在C++中,下面三种对象需要调用拷贝构造函数(有时也称“复制构造函数”): 1) 一个对象作为函数参数,以值传递的方式传入函数体: 2) 一个对象作为函数返回值,以值传递的方式从函数返回: 3) 一个 ...
- [ An Ac a Day ^_^ ] CodeForces 677B Vanya and Food Processor 模拟
题意: 你有一个榨汁机 还有n个土豆 榨汁机可以容纳h高的土豆 每秒可以榨k高的东西 问按顺序榨完土豆要多久 思路: 直接模拟 一开始以为是最短时间排了个序 后来发现多余了…… #include< ...
- NOIP2010-普及组复赛模拟试题-第一题-手机
题目背景 Background 现在手机使用越来越广泛了 题目描述 Description 一般的手机的键盘是这样的: 要按出英文字母就必须要按数字键多下.例如要按出 x 就得按 9 两下,第一 ...