Lambda表达式对整数类型数组的一些基本操作
一.求两个集合的交集,并集,差集。
1.求两个集合的交集
int[] A = {1, 2, 3, 4};
int[] B = {2, 4, 7, 8, 9};
IEnumerable<int> enumerable = A.Intersect(B);
输出结果:

Intersect()方法:通过使用默认的相等比较器对值进行比较生成两个序列的交集。
2.求两个集合的并集
int[] A = {1, 2, 3, 4};
int[] B = {2, 4, 7, 8, 9};
IEnumerable<int> enumerable = A.Union(B);
输出结果:

Union()方法:通过使用默认的相等比较器生成两个序列的并集。
3.求两个集合的差集
int[] A = {1, 2, 3, 4};
int[] B = {2, 4, 7, 8, 9};
(1)在B去掉A中的元素。
IEnumerable<int> enumerable = B.Except(A);
输出结果:

(2)在A中去掉B的元素
IEnumerable<int> enumerable = A.Except(B);
输出结果:


浙公网安备 33010602011771号