黑马程序员-------.net基础知识九

来源:互联网 发布:淘宝联盟手机版5.2 编辑:程序博客网 时间:2024/05/16 10:54


---------------------- ASP.Net+Android+IO开发S.Net培训、期待与您交流! ----------------------

 

C#集合类: Array .ArrayList .Dictionary 相关

    我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类。我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转  化,增加了系统装箱和拆箱的负担,例如我们需要在电子商务网站中存储用户的购物车信息(商品名,对应的商品个数)时,完全可以用 Dictionary<string, int> 来存储购物车信息,而不需要任何的类型转化。
 1.数组是固定大小的,不能伸缩。虽然System.Array.Resize这个泛型方法可以重置数组大小,但是该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃!而集合却是可变长的
2.数组要声明元素的类型,集合类的元素类型却是object.
3.数组可读可写不能声明只读数组。集合类可以提供ReadOnly方法以只读方式使用集合。
4.数组要有整数下标才能访问特定的元素,然而很多时候这样的下标并不是很有用。集合也是数据列表却不使用下标访问。很多时候集合有定制的下标类型,对于队列和栈根本就不支持下标访问!


数组 Array

int[] intArray1;//初始化已声明的一维数组intArray1 = new int[3];intArray1 = new int[3]{1,2,3};intArray1 = new int[]{1,2,3};

ArrayList类  

对象被设计成为一个动态数组类型,其容量会随着需要而适当的扩充.

方法:

1:Add()向数组中添加一个元素,

2:Remove()删除数组中的一个元素

3:RemoveAt(int i)删除数组中索引值为i的元素

4:Reverse()反转数组的元素

5:Sort()以从小到大的顺序排列数组的元素

6:Clone()复制一个数组

例:

using System;using System.Collections.Generic;using System.Text;using System.Collections;namespace ConsoleApplication1{     class Program      {         static void Main(string[] args)          {             ArrayList al = new ArrayList();             al.Add(100);//单个添加              foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })              {                 al.Add(number);//集体添加方法一             }              int[] number2 = new int[2] { 11,12 };             al.AddRange(number2);//集体添加方法二             al.Remove(3);//移除值为3的             al.RemoveAt(3);//移除第3个             ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份             Console.WriteLine("遍历方法一:");             foreach (int i in al)//不要强制转换              {                 Console.WriteLine(i);//遍历方法一             }             Console.WriteLine("遍历方法二:");             for (int i = 0; i != al2.Count; i++)//数组是length              {                 int number = (int)al2;//一定要强制转换                 Console.WriteLine(number);//遍历方法二             }         }     }}


List(泛型集合)和常用方法汇总及举例  

 

 1 Add 将对象添加到List<T>的结尾处

例:

List<int> list = new List<int>();            list.Add(12);            list.Add(56);            list.Add(0);            list.Add(91);            list.Add(6);            for (int i = 0; i < list.Count; i++)            {                Console.WriteLine(list[i]);            }            Console.ReadKey();

2 AddRange  将指定集合的元素添加到List<T>当中。

例:

List<int> list = new List<int>();            int[] nums = { 1, 3, 5, 7, 9 };            list.Add(12);            list.Add(56);            list.Add(0);            list.Add(91);            list.Add(6);            list.AddRange(nums);   //数组中的值以单个值的形式存入泛型集合中            for (int i = 0; i < list.Count; i++)            {                Console.WriteLine(list[i]);            }            Console.ReadKey();

3 Clear 从List<T>中移除所有元素

例:

List<int> list = new List<int>();            int[] nums = { 1, 3, 5, 7, 9 };            list.Add(12);            list.Add(56);            list.Add(0);            list.Add(91);            list.Add(6);            list.AddRange(nums);   //数组中的值以单个值的形式存入泛型集合中            list.Clear();            for (int i = 0; i < list.Count; i++)            {                Console.WriteLine(list[i]);            }            Console.ReadKey();

4 contains  判断元素是否存在于List<T>中

例:

List<int> list = new List<int>();            int[] nums = { 1, 3, 5, 7, 9 };            list.Add(12);            list.Add(56);            list.Add(0);            list.Add(91);            list.Add(6);            list.AddRange(nums);   //数组中的值以单个值的形式存入泛型集合中            bool result = list.Contains(9);            Console.WriteLine(result);   //返回true            Console.ReadKey();


5 Equals(object) 确定指定的object是否等于当前的object。(继承自object)

例:

List<int> list = new List<int>();            List<int> list2 = new List<int>();            int[] nums = { 12, 56, 0, 91, 6 };            list.Add(12);            list.Add(56);            list.Add(0);            list.Add(91);            list.Add(6);            list2.AddRange(nums);   //数组中的值以单个值的形式存入泛型集合中            bool result = list.Equals(list2);            Console.WriteLine(result);


对于直接应用中的例子:

//    先定义了一个类:public class Student    {        public string Name { get; set; }        public int Age { get; set; }        public Student(string name, int age)        {            this.Age = age;            this.Name = name;        }    }   list<T>实例:            List<Student> stu = new List<Student>();            Student stu1 = new Student("小华", 12);            Student stu2 = new Student("小定", 12);            Student stu3 = new Student("小砍", 12);            Student stu4 = new Student("小苗", 12);            stu.Add(stu1);            stu.Add(stu2);            stu.Add(stu3);            stu.Add(stu4);            foreach (Student student in stu)            {                Console.WriteLine("{0} {1}",student.Name,student.Age);            }            Console.ReadKey();


Dictionary

 Dictionary<K,V>泛型集合不仅对数据类型做了约束,他可以对类进行操作,遍历此集合时,是对它的value进行遍历。还能通过关键字Key来访问集合。

例: 

Dictionary<string,Student> stu = new Dictionary<string,Student>();                        Student stu1 = new Student("小华", 12);            Student stu2 = new Student("小定", 12);            Student stu3 = new Student("小砍", 12);            Student stu4 = new Student("小苗", 12);            stu.Add(stu1.Name,stu1);            stu.Add(stu2.Name,stu2);            stu.Add(stu3.Name,stu3);            stu.Add(stu4.Name,stu4);            Student stu5 = stu["小华"];//能通过key关键字来访问            foreach (Student student in stu.Values)            {                Console.WriteLine("{0} {1}  {2}",student.Name,student.Age,stu5.Age);            }            Console.ReadKey();


 

 

 

 

 

 
---------------------- ASP.Net+Android+IO开发S.Net培训、期待与您交流! ----------------------