常用集合及其主要方法

C#中集合主要有泛型集合和非泛型集合。

主要非泛型集合:

ArrayList:动态数组,能够自动增长。

常用方法:Add(object value);Remove(object value);RemoveAt(int index);RemoveRange(int index,int count);Insert(int index,object value);Sort();Reverse();IndexOf(object);Contains(object);Clear();ToArray();

ArrayList集合遍历的三种方式:

1.

for(int i=0;i<list.count;i++)                                       

{

    Console.Write(list[i].ToString()+",");

}

2.

扫描二维码关注公众号,回复: 3492056 查看本文章

IEnumertor num=list.GetEnumertor();

   while(num.MoveNext())

{

Console.Write(num.Curret.ToString()+"");

}  

3.

foreach(var item in list)

{

 Console.writer(item.ToString()+"");
}

泛型集合

List<T>集合

常用方法:

01. Add    将对象添加到 List<T> 的结尾处。

02. AddRange    将指定集合的元素添加到 List<T> 的末尾。

03. AsReadOnly    返回当前集合的只读 IList<T> 包装。  

04. BinarySearch(T)    使用默认的比较器在整个已排序的 List<T> 中搜索元素,并返回该元素从零开始的索引。  

05. BinarySearch(T, IComparer<T>)   使用指定的比较器在整个已排序的 List<T> 中搜索元素,并返回该元素从零开始的索引。  

 06 . BinarySearch(Int32, Int32, T, IComparer<T>)  使用指定的比较器在已排序 List<T> 的某个元素范围中搜索元素,并返回该元素从零开始的索引。   

 07. Clear    从 List<T> 中移除所有元素。    

08. Contains    确定某元素是否在 List<T> 中。  

09. ConvertAll<TOutput>  将当前 List<T> 中的元素转换为另一种类型,并返回包含转换后的元素的列表。

10. CopyTo(T[])    将整个 List<T> 复制到兼容的一维数组中,从目标数组的开头开始放置。  

11. Exists     确定 List<T> 是否包含与指定谓词所定义的条件相匹配的元素。

12. Find   搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中的第一个匹配 元素。  

13 .FindIndex(Predicate<T>)    搜索与指定谓词所定义的条件相匹配的元素,并返回整个List <T> 中第一个匹配元素的从零开始的索引。  

14. ForEach   对 List<T> 的每个元素执行指定操作。  GetEnumerator    返回循环访问 List<T> 的枚举器。  

15 . IndexOf(T) 搜索指定的对象,并返回整个 List<T> 中第一个匹配项的从零开始的索引。  

16. Insert    将元素插入 List<T> 的指定索引处。

17. InsertRange    将集合中的某个元素插入 List<T> 的指定索引处。    

18. LastIndexOf(T)    搜索指定的对象,并返回整个 List<T> 中最后一个匹配项的从零开始的索引。    

19. Remove    从 List<T> 中移除特定对象的第一个匹配项。    

20. Reverse()     将整个 List<T> 中元素的顺序反转。    

21. Sort()     使用默认比较器对整个 List<T> 中的元素进行排序。  

List<T>集合遍历的二种方法:类似ArrayList

Dictionary<K,V> 集合
  1. 创建及初始化

     Dictionary<int,string>myDictionary=newDictionary<int,string>();

    添加元素

    myDictionary.Add(1,"C#");

    myDictionary.Add(2,"C++");

    myDictionary.Add(3,"ASP.NET");

    myDictionary.Add(4,"MVC");

  2. 通过Key查找元素

    if(myDictionary.ContainsKey(1))

    {

    Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]);

     }

    通过Remove方法移除指定的键值

    myDictionary.Remove(1);

    if(myDictionary.ContainsKey(1))

    ...{

     Console.WriteLine("Key:{0},Value:{1}","1", myDictionary[1]);

      }

        else

       {

      Console.WriteLine("不存在 Key : 1"); 

     }

   Dictionary<T>集合遍历的方式:
  1.  通过KeyValuePair遍历元素

    foreach(KeyValuePair<int,string>kvp in myDictionary)

    ...{

    Console.WriteLine("Key = {0}, Value = {1}",kvp.Key, kvp.Value);

    }

    仅遍历键 Keys 属性

    Dictionary<int,string>.KeyCollection keyCol=myDictionary.Keys;

    foreach(intkeyinkeyCol)

    ...{

    Console.WriteLine("Key = {0}", key);

    }

    仅遍历值 Valus属性

    Dictionary<int,string>.ValueCollection valueCol=myDictionary.Values;

    foreach(stringvalueinvalueCol)

    ...{

    Console.WriteLine("Value = {0}", value);

    }

 

猜你喜欢

转载自www.cnblogs.com/xiaoshizi/p/9759485.html
今日推荐