深入.net(集合)

來源:互聯網
上載者:User

標籤:

 Java培訓、Android培訓、iOS培訓、.Net培訓、期待與您交流!

 

 

 集合技術:

         用於“批量資料”管理的重要技術,是數組技術的替代技術!

        與數組技術的對比:

         數組:只提供“儲存的空間”,但缺乏各種資料管理措施!

         集合:在數組的基礎上,提供豐富的“屬性”和“方法”,來方便我們對資料的訪問,且不限定長度!

 

典型“集合類型”:

        傳統集合  ----- ArrayList、Hashtable (靈活、需要裝拆箱、提取資料容易拆箱錯誤)

        泛型集合  -----List<T>、      Dictionary<K,V>(減少靈活性,確保穩定性且提高效率)

 

 

集合中,重要的屬性和方法:

        ----- ArrayList aList = new ArrayList(100);//一般不指定長度

泛型集合 的使用(List<T>):

        屬性、方法:與傳統集合ArrayList一樣,唯一不同的是,限定資料類型,不需再進行裝、拆箱!

 

 

List<T>的使用文法:

               //初始化方式一:

            _studens = new List<Student>();//1、準備容器

            Student stu1 = new Student(); //2、建立學生對象

            stu1.Name = "王小平";

            stu1.Age = 20;

            stu1.Sex = "男";

            _studens.Add(stu1);//3、加入集合

            //初始化方式二:

            _studens = new List<Student>()

            {

                new Student(){Name="王軍", Age=20, Sex="男"},

                new Student(){Name="白小鵬",Age=30, Sex="男"},

                new Student(){Name="劉麗",Age=18,Sex="女"}

            };

              //資料繫結方式一:BindingList

            this.dataGridView1.DataSource =

                new BindingList<Student>(_studens);

 

            //資料繫結方式二:BindingSource

            BindingSource source = new BindingSource();

            source.DataSource = _studens;

            this.dataGridView1.DataSource = source;

 

Dictionary<k,v>的使用文法:

               //初始方式一

            _student = new Dictionary<string, Student>();

            Student stu1 = new Student();

            stu1.Name = "王大力";

            stu1.Age = 23;

            stu1.Sex = "男";

            _student.Add(stu1.Name, stu1);

            //初始方式二(集合初始化化器)

            _student = new Dictionary<string, Student>()

            {

                {"王大力",new Student(){Name="王大力",Age=23,Sex="男"}},

                {"白軍",new Student(){Name="白軍",Age=21,Sex="男"}},

                {"李白",new Student(){Name="李白",Age=27,Sex="男"}}

            };

            //資料繫結

            BindingSource source = new BindingSource();

            source.DataSource = _student.Values;

            this.dataGridView1.DataSource = source;

 

 

 

 

ArrayList集合,替代了數組,提供大量的屬性和方法來方便使用者的操作!但是,當要元素搜尋定位時,還是只有通過“迴圈遍曆”的方式,如何最佳化呢?

        ----- 不在將資料“順序存放”,而是在資料存放前,通過其“能夠標識其身份的資訊”(key鍵),通過公式(雜湊演算法)計算出它的存放地址!這樣的好處就是,當需要尋找某一元素時,只需要知道(key鍵),就可以通過雜湊演算法,再次算出它的存放地址,從而快速定位元素。【Hashtable】

 

深入.net(集合)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.