本文翻译自:Joining two lists together

If I have two lists of type string (or any other type), what is a quick way of joining the two lists? 如果我有两个类型字符串列表(或任何其他类型),加入这两个列表的快速方法是什么?

The order should stay the same. 订单应该保持不变。 Duplicates should be removed (though every item in both links are unique). 应删除重复项(尽管两个链接中的每个项都是唯一的)。 I didn't find much on this when googling and didn't want to implement any .NET interfaces for speed of delivery. 谷歌搜索时我没有找到太多,并且不想实现任何.NET接口以提高交付速度。


#1楼

参考:https://stackoom.com/question/6PXv/将两个列表连接在一起


#2楼

Something like this: 像这样的东西:

firstList.AddRange (secondList);

Or, you can use the 'Union' extension method that is defined in System.Linq. 或者,您可以使用System.Linq中定义的“Union”扩展方法。 With 'Union', you can also specify a comparer, which can be used to specify whether an item should be unioned or not. 使用“Union”,您还可以指定比较器,该比较器可用于指定项目是否应该联合。

Like this: 像这样:

List<int> one = new List<int> { 1, 2, 3, 4, 5 };
List<int> second=new List<int> { 1, 2, 5, 6 };var result = one.Union (second, new EqComparer ());foreach( int x in result )
{Console.WriteLine (x);
}
Console.ReadLine ();#region IEqualityComparer<int> Members
public class EqComparer : IEqualityComparer<int>
{public bool Equals( int x, int y ){return x == y;}public int GetHashCode( int obj ){return obj.GetHashCode ();}
}
#endregion

#3楼

You could try: 你可以尝试:

List<string> a = new List<string>();
List<string> b = new List<string>();a.AddRange(b);

MSDN page for AddRange AddRange MSDN页面

This preserves the order of the lists, but it doesn't remove any duplicates which Union would do. 这保留了列表的顺序,但它不会删除Union所做的任何重复。

This does change list a . 这确实改变了列表a If you wanted to preserve the original lists then you should use Concat (as pointed out in the other answers): 如果你想保留原始列表,那么你应该使用Concat (如其他答案中所指出的):

var newList = a.Concat(b);

This returns an IEnumerable as long as a is not null. 只要a不为null,这将返回IEnumerable


#4楼

The Union method might address your needs. Union方法可能会满足您的需求。 You didn't specify whether order or duplicates was important. 您没有指定订单或重复项是否重要。

Take two IEnumerables and perform a union as seen here: 拿两个IEnumerables并执行一个联合,如下所示:

int[] ints1 = { 5, 3, 9, 7, 5, 9, 3, 7 };
int[] ints2 = { 8, 3, 6, 4, 4, 9, 1, 0 };IEnumerable<int> union = ints1.Union(ints2);// yields { 5, 3, 9, 7, 8, 6, 4, 1, 0 }

#5楼

单向:List.AddRange()取决于类型?


#6楼

AddRange方法

aList.AddRange( anotherList );

将两个列表连接在一起相关推荐

  1. 两个列表合并去重_数据结构——列表

    4.1列表的概念 列表(list)是用来存储一组有序数据元素的数据结构,元素之间用逗号分隔.列表中的数据元素应该包括在方括号中,而且列表是可变的数据类型,一旦创建了一个列表,你可以添加.删除或者搜索列 ...

  2. 如何在Python中串联两个列表?

    如何在Python中串联两个列表? 例: listone = [1, 2, 3] listtwo = [4, 5, 6] 预期结果: >>> joinedlist [1, 2, 3, ...

  3. python列表连接_Python连接列表

    python列表连接 Python join list means concatenating a list of strings with a specified delimiter to form ...

  4. 远程控制软件如何实现两台电脑连接

    目前,笔记本电脑可以说在生活中起到了很大的作用,因为它携带很方便,可以随时随地工作,可以说起到了移动办公桥梁的作用.特别是现在远程办公慢慢的走进了人们的生活中,已经变成了一部分人生活的一部分,其实远程 ...

  5. python输入两个字符串连接起来_python字符串连接的多种方法

    python中有很多字符串连接方式,今天在写代码,顺便总结一下,从最原始的字符串连接方式到字符串列表连接,大家感受下 python中有很多字符串连接方式,今天在写代码,顺便总结一下: 最原始的字符串连 ...

  6. LeetCode简单题之两个列表的最小索引总和

    题目 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一个 ...

  7. reportConfig.xml两种数据源连接的配置方式

     在reportConfig.xml配置文件中,我们提供了两种数据源连接的配置方式,分别如下: 1.jndi数据源配置(即:在dataSource中配置) 此配置适用于在j2ee的服务器中配置了j ...

  8. 【每日一算法】两个列表的最小索引总和

    微信改版,加星标不迷路! 每日一算法-两个列表的最小索引总和 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的 ...

  9. 快速找出两个列表差异部分

    2019独角兽企业重金招聘Python工程师标准>>> int [] arr0 = new int [] {1 ,5 ,12 ,21 ,11 ,15 ,8 ,30} ;int [] ...

最新文章

  1. MTD的坏块管理(一)-快速了解MTD的坏块管理
  2. 被誉为「教科书」,牛津大学231页博士论文全面阐述神经微分方程,Jeff Dean点赞...
  3. Newtonsoft.Json code
  4. [洛谷P5137]polynomial
  5. 对javascript作用域链的理解
  6. CET6级高频词(按频度)(700个)
  7. OpenCV4.4.0+VS2017 环境配置
  8. java 减少内存_java中减少内存占用小技巧
  9. Protobuf序列化的原理-负数的存储
  10. Docker镜像的多平台架构支持
  11. 【程序员】保持一颗虚心好学的心态去敲代码
  12. 人生价值观的培养和建立
  13. 【Spring 基础注解】对象创建相关注解、注入相关注解、注解扫描详解
  14. 不止 RTC 技术盛会,你还应该知道的声网给开发者的福利
  15. python基础运用_python基础----python的使用(三)
  16. Mac-录屏软件-视频转gif动图
  17. matlab yalmip cplex,matlab – CPLEX YALMIP – “未找到解算器”?
  18. 数学建模学习笔记(十七)传染病模型(SIER)
  19. 红米Note5官方刷机日记 - 小米助手Recovery刷机
  20. 成都Uber优步司机奖励政策(3月10日)

热门文章

  1. Android camera开发总结
  2. SQLite的锁的原理:
  3. ThreadLocal 原理 以及设计思想
  4. 算法-------矩阵中的最长递增路径(Java版本)
  5. 曹新雨-2020年目标
  6. 提供推荐——协作型过滤
  7. android GLSurfaceView渲染模式
  8. Spark Shuffle两种Manager
  9. Hive运行方式、gui
  10. 【Java基础】对象拷贝