码迷,mamicode.com
首页 > Web开发 > 详细

深入.net(集合)

时间:2015-02-10 15:08:20      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

 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(集合)

标签:

原文地址:http://www.cnblogs.com/why168888/p/4283596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!