欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

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

来源:本站原创|时间:2020-01-10|栏目: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

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

上一篇:详解C# Socket异步通信实例

栏    目:C#教程

下一篇:详解C#对XML、JSON等格式的解析

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

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/6101.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有