转 C#对多个集合和数组的操作(合并,去重,判断)
生活随笔
收集整理的這篇文章主要介紹了
转 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
轉載于:https://www.cnblogs.com/luoluoluoD/p/6813923.html
總結
以上是生活随笔為你收集整理的转 C#对多个集合和数组的操作(合并,去重,判断)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 运输问题(模型建立、表上作业法、产销平衡
- 下一篇: sikuli+eclipse对于安卓ap