当前位置: 移动技术网 > IT编程>开发语言>c# > C#对多个集合和数组的操作方法(合并,去重,判断)

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

在开发过程中.数组和集合的处理是最让我们担心.一般会用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

以上这篇c#对多个集合和数组的操作方法(合并,去重,判断)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网