zoukankan      html  css  js  c++  java
  • C# 數據集合---2.列表、字典

    列表list,list<T>,SortedList

    list<T>
    ArrayList 不是泛型,這種集合可以存放任意類型數據,list<T>是泛型集合,規定了集合內的數據類型,只能存放<T>的T類型數據
    添加元素:
    * List.Add(T item)  添加一個元素   mList.Add("yao")
    * List.AddRange(IEnumerable<T>collection) 添加一組元素    string[]temArr = {"a","b","c"} mList.AddRange(temArr)
    * 在index位置添加一個元素 mList.Insert(1,"aa"); 

    刪除元素:

    * 刪除一個值  mList.Remove("YAO");
    * 刪除下表為index的元素  mList.RemoveAt(0);
    * 從下標index開始,刪除count個元素   mList.RemoveRange(1,2);

    排序元素:

    默認是元素第一個字母升序 mList.Sort();
    順序反轉  List.Reverse();

    查找元素:

    List.Find  返回List的第一個匹配元素
    List.FindLast  返回List中的最後一個匹配元素
    List.TrueForAll 確定是否List中的每個元素都與指定的謂詞所定義的條件相匹配
    List.FindAll 檢索指定詞 所定義條件相匹配的所有元素
    List.Where   檢索指定詞 所定義條件相匹配的所有元素
    List.RemoveAll  移除指定詞所定義的條件相匹配的所有元素

    排序列表(SortedList)

    表示鍵/值對的集合,這些鍵和值按鍵排序 ,並且可以按照鍵和索引訪問

    屬性:
    capacity   獲取或這隻容量
    count    獲取元素個數
    IsFixedSize    獲取一個值表示是否具有固定大小
    IsReadOnly 獲取一個值表示是否只讀
    Item 獲取或設置與 sortedlist中指定的鍵相關方的值
    Keys 獲取鍵
    Values  獲取值
     
    方法:
    添加刪除  public virtual void Add(object key,obiect cvalue)     ...Clear();
    public virtual Clear()
    判斷是否包含指定的鍵/值  public virtual bool    ContainKey/value(object key/value);
    獲取指定索引處的值/鍵   public virtual object GetByIndex/GetKey(int index)
    namespace List
    {
        class List
        {
            static void Main(string[] args)
            {
                List<string> lt = new List<string>();//創建一個list數據集合
                lt.Add("A");//添加元素
                 lt.Add("B");
                 lt.Add("C");
                 lt.Add("D");
                Console.WriteLine();
                foreach(string lo in lt)
                {
                    Console.WriteLine(lo);
                }
                Console.WriteLine("容量Capacity:{0}",lt.Capacity);//輸出集合容量
                Console.WriteLine("數量Count: {0}",lt.Count);//輸出元素數量
                Console.WriteLine("A元素是否存在Contains:{0}",lt.Contains("A"));//判斷是否包含某個元素
                lt.Insert(2,"z");//將元素插入到集合的指定索引處
                Console.WriteLine();
                foreach(string lo in lt)  //打印集合中元素
                {
                    Console.WriteLine(lo);
                }
                Console.WriteLine("lt[3]:{0}",lt[3]);//輸出集合中索引為3的元素
                Console.ReadKey();
            }
        }
    }
    
    A
    B
    C
    D
    容量Capacity:4
    數量Count: 4
    A元素是否存在Contains:True
    
    A
    B
    z
    C
    D
    lt[3]:C
    三、字典 Dictionary<k,v>,SortedDictionary
    字典表示一種複雜的數據結構,允許按照某個鍵來訪問元素,字典也稱為映射或散列表
    字典根據鍵快速查找值,也可以自由添加和刪除元素,與List<T>類似但沒有在內存中移動後續元素的性能開銷
    字典每個元素都是一個鍵值對(鍵和值)
    鍵必須唯一,值不需要唯一,鍵和值可以是任意類型
    Dictionary<k,v>
    常用屬性:
    Comparer    獲取用於確定字典中的鍵是否相等的IEqualityComParer<T>
    Count    獲取包含在Dictionary<Tkey,Tvalue>中的鍵/值對的數目
    Item    獲取或設置與指定的鍵相關聯的值
    Keys / Values   獲取包含Dictionary<TKey,TValue>中的鍵 / 值的集合
     
    常用方法:
    Add    將指定的鍵和值添加到字典中
    Remove    移除所指定的鍵和值
    ToString    返回當前對象的字符串
    TryGetValue    獲取與指定的鍵相關聯的值
    Clear    從Dictionary<TKey,TValue>中移除所有的鍵和值
    ContainsKey  / ContainsValue  確定Dictionary<Tkey,TValue>是否包含指定的鍵/值
    Equals(object)    確定指定的Object是否等於當前的object
    Finalize    允許對象在“垃圾回收”回收之前嘗試釋放資源并執行其他清理操作
    GetEnumerator    返回循環訪問Dictionary<TKey,TValue>的枚舉器
    GetHasCode    用作特定類型的哈希函數
    GetObjectData 實現System.Runtime.Serialization.ISerializable接口,并返回序列化Dictionary<Tkey,TValue>實例所需的數據
    GetType        獲取當前實例的Type
    實例:
    namespace Dictionary
    {
        class Program
        {
            /// <summary>
            /// 字典Dictionary
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                //定義
                Dictionary<string, string> openWith = new Dictionary<string, string>();
                Dictionary<int, string[]> OtherType = new Dictionary<int, string[]>();
                //添加元素
                openWith.Add("a", "1");
                openWith.Add("b", "2");
                openWith.Add("c", "3");
                openWith.Add("d", "4");
                // 刪除元素
                openWith.Remove("b");
                if(!openWith.ContainsKey("b"))
                {
                    Console.WriteLine("doc not found");
                }
               
                //取值
                Console.WriteLine("key = "a",value={0}.",openWith["a"]);
                //遍歷Key
                foreach (var item in openWith.Keys)
                {
                    Console.WriteLine("Key = {0}", item);
                }
                //遍歷value
                foreach(var item in openWith.Values)
                {
                    Console.WriteLine("value={0}",item);
                }
                //遍歷value的第二種方法
                Dictionary<string, string>.ValueCollection valueColl = openWith.Values;
                foreach (var item in valueColl)
                {
                    Console.WriteLine("value = {0}",item);
                }
                //遍歷字典
                foreach (KeyValuePair<string, string> item in openWith)
                {
                    Console.WriteLine("key = {0},value ={1}",item.Key, item.Value);
                }
                //參數為自定義類型,聲明并添加元素
                Dictionary<int, Custom> MyType = new Dictionary<int, Custom>();
                for (int i = 1; i <= 9;i++ )
                {
                    Custom element = new Custom();
                    element.Code = i * 100;
                    MyType.Add(i, element);
                }
                Console.ReadKey();
            }
        }
        public class Custom
        {
            public int Code { get { return _Code; } set { _Code = value; } }
            private int _Code;
        }
    }
    SortedDictionary  有序字典
    SortedDictionary 與 SortedList使用方法一樣區別在於
    sortedList類使用的內存比sorteddictionary類少
    sortedDictionary類的插入和刪除速度比較快
    在已排序好的數據填充集合石,若不需要修改容量,sortedList類就比較快
     
    SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
                sd.Add("1", "aa");
                sd.Add("a", "cc");
                sd.Add("3", "vv");
                sd.Add("2", "dd");
                foreach (KeyValuePair<string, string> item in sd)
                {
                    Console.WriteLine("鍵名:" + item.Key + "鍵值:" + item.Value);
                }
    輸出
    鍵名:1鍵值:aa
    鍵名:2鍵值:dd
    鍵名:3鍵值:vv
    鍵名:a鍵值:cc
  • 相关阅读:
    [转]Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)
    [转]OAuth 2.0
    SpringMVC之七:SpringMVC中使用Interceptor拦截器
    多数据源问题--Spring+Ibatis 访问多个数据源(非分布式事务)
    读写分离
    SVN中检出(check out) 和 导出(export) 的区别
    Hbase之三:Hbase Shell使用入门
    hadoop之一:概念和整体架构
    Twitter Storm如何保证消息不丢失
    Twitter Storm: storm的一些常见模式
  • 原文地址:https://www.cnblogs.com/ygtup/p/9359116.html
Copyright © 2011-2022 走看看