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. 实验吧Web-FALSE

    笔记: PHP函数isset(): 检测变量是否设置 只能用于变量,传递任何其它参数都将造成解析错误.若想检测常量是否已设置,可使用 defined() 函数 格式:  isset ( mixed v ...

  2. 玩玩RMI

    今天在看代理设计模式,java中远程代理的实现一定会用到RMI的,很久没有温习过RMI的知识了,今天就重新过一遍这个知识点来让自己加深印象,构建一个简单的RMI小程序需要用到一下几个类: java.r ...

  3. Oberon程序设计—目录

    内        容前   言1, 什么是Oberon? 1.1 ALGOL家族 1.2 该系统2, 第一:程序 2.1 一个符号来描述的语法: 2.2练习 第一部分,符号和基本类型,分配,控制结构, ...

  4. LeetCode OJ 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. ckeditor 基础

    <!DOCTYPE html> <!-- Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights rese ...

  6. 在windows上部署使用Redis(摘录)

    下载Redis 在Redis的官网下载页上有各种各样的版本,我这次是在windows上部署的,要去GitHub上下载.目前的是2.8.12版的,直接解压,在\bin\release 目录下有个压缩包, ...

  7. erlang分布式编程模型

    erlang分布式编程有两种模型 一.分布式erlang 运行在可信的网络环境中 1.rpc提供的远程过程调用 rpc:call(Node,Mode,Fun,Args) ->Result|{ba ...

  8. 杭电20题 Human Gene Functions

    Problem Description It is well known that a human gene can be considered as a sequence, consisting o ...

  9. FreeMarker 小结

    一.Sequence 的内置函数1.sequence?first 返回sequence 的第一个值.2.sequence?last 返回sequence 的最后一个值.3.sequence?rever ...

  10. Activity之间的隐士跳转

    /**             * 方法一:在构造函数中指定             */            /*Intent intent=new Intent(this,TwoActivity ...