软件编程
位置:首页>> 软件编程>> C#编程>> C#对多个集合和数组的操作方法(合并,去重,判断)

C#对多个集合和数组的操作方法(合并,去重,判断)

作者:jingxian  发布时间:2021-12-11 14:02:44 

标签:C#,数组,操作,集合

在开发过程中.数组和集合的处理是最让我们担心.一般会用for or foreach 来处理一些操作.这里介绍一些常用的集合跟数组的操作函数.

首先举例2个集合A,B.

List<int> listA = new List<int> {1,2,3,5,7,9};

List<int> listB = new List<int> {13,4,17,29,2};

listA.AddRange(listB ); 把集合A.B合并

List<int> Result = listA.Union(listB).ToList<int>(); //剔除重复项

List<int> Result = listA.Concat(listB).ToList<int>(); //保留重复项

listA.BinarySearch("1"); //判断集合中是否包含某个值.如果包含则返回0

在举例两个数组

int[] i=new int[]{1,2};
int[] j=new int[]{2,3};
List<int> r = new List<int>();
r.AddRange(i);

r.AddRange(j);
int[] c = r.ToArray(); 合并数组

int[] x=i.Union(j).ToArray<int>(); //剔除重复项

int[] x=i.Concat(j).ToArray<int>(); //保留重复项

int n = Array.BinarySearch(i,3);//判断数组中是否包含某个值.如果包含则返回0

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com