主要笔记:
使用System.Array操作数组
使用System.ArrayList对象
使用System.Hashtable对象
理解泛型集合类List<I>对象
掌握Dictionary<TKey,TValue>
集合的功能:存储数据,提供增,删,改,查操作,长度可以自动增长
集合的接口:
ICollection:定义了集合的基本规范,所有的集合都要实现该接口
IEnumerator:枚举接口,一个类实现了IEnumerator,也就是实现了Current属性,MoveNext方法,Reset方法,只要实现这些方法,那么就能使用foreach这种语法了
IEnumerable:枚举接口,主要实现GetEnumerator方法,该方法返回一个IEnumerator,一个类实现了IEnumerable接口后,调用foreach语法的时候,会自动调用GetEnumerator方法,然后在这个IEnumerator中遍历
IList:有序的单对象存储接口,继承了ICollection和IEnumerable
IDictionary:字典接口表示存储的数据是键值对
System.Array介绍
Array是抽象的基类,提供CreateInstance的方法来创建数组
语句:Array str=Array.CreateInstance(typeof(string),5,3);(注意:代码中的typeof关键字用于获取某种数据类型的System.Type对象。可将代码中的int和string换成其他的数据类型)
列举一些Array中的属性和方法
length:得到数组所有唯元素总个数的属性
实例方法:
GetValue():通过索引返回指定元素的值
SetValue():将数组中的指定元素设为指定值
静态方法:
Sort():将数组中的元素进行排序,只能对一维数组从小到大排序
System.ArrayList的介绍
ArrayList是Array的优化版,前者提供了大部分集合类具有的而后者没有的特色
Array同时也具有ArrayList的灵活性:
Array的下标可以设置,而ArrayList的下标始终是0;
Array可以是多维的,而ArrayList始终是一维的
ArrayList的实例语法:Arraylist 对象名称=new ArrayList([初始容量]);
ArrayList的属性和方法:
属性:Count 数组列表中的元素的实际个数
方法:
Add()在数组列表的尾部追加元素;Remove() 从数组列表中移除第一次出现的给定元素;RemoveAt() 移出数组列表中指定索引处的元素
特点:1、实现了IList接口 ;2、 可 以存储任意类型的数据;3、有序,可以通过索引访问数据,访问的数据和类型是object;4、可以重复存储同一个对象
Hashtable类的介绍
Hashtable的特点:
1、存储的是键值对;2、无序存储,不能通过索引访问;3、一般通过键访问值
keys:存储的是键的集合 Values:存储的是值的集合
泛型集合:明确指定了要放入集合的对象是何种类型的集合
列举一些泛型集合类
List<T>类:泛型集合类<数据类型> 实例名=new 泛型集合类<数据类型>(); 在此类中,不仅可以实现添加和访问元素、插入和删除元素、清空集合、把元素复制到数组中,还可以搜索和转换元素、使元素逆序等高级操作。
using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace _4_1 { class Program { static void Main(string[] args) { int[] aa = new int[5]; aa[0] = 23; aa[1] = 34; aa[2] = 32; aa[3] = 33; aa[4] = 45; //aa[5] = 66;//错误的,数组一旦实例化长度就固定了,所以超出了数组的长度 Console.WriteLine(aa[2]);//使用下标访问数组,类型是确定的 //--------Array数组的使用// Array ay = Array.CreateInstance(typeof(int), 5); ay.SetValue(23, 0); ay.SetValue(24, 1); ay.SetValue(28, 2); ay.SetValue(63, 3); ay.SetValue(21, 4); Console.WriteLine( ay.GetValue(3)); Console.WriteLine( "------下面是集合案例----------------"); ArrayList list = new ArrayList(5);//capacity:表示集合的初始容量 list.Add(1); list.Add("asdf"); list.Add('f'); list.Add(true); list.Add(new Student("张三", 23)); list.Add(new Student("张三", 23)); Console.WriteLine("集合的当前容量:"+list.Capacity); Console.WriteLine("集合中的数据个数:"+list.Count); ///count:表示集合中存储的对象的个数 Console.WriteLine(list[2]);//按位置(索引)访问数据,数据类型是object 要使用必须强制转换 list.Insert(2, 5);//插入数据,第一个参数表示插入的索引 list.Remove('f'); list.Remove(new Student("张三", 23)); list.RemoveAt(2);//移除第三个位置的对象 for(int i=0;i<list.Count;i++) Console.WriteLine(list[i]); foreach (object obj in list) Console.WriteLine(obj); list.Clear();//清除集合里的所有对象; Console.WriteLine("集合中的数据个数:" + list.Count); Console.WriteLine("------下面是HashTable集合案例----------------"); Hashtable ht = new Hashtable(); ht.Add(1, 5); ht.Add(4, 8); ht.Add(new Student("sss", 23), "dsfgdgf"); ht.Add(3, 7); ht.Add(2,15); ht.Add("asd", "wetretretre"); Console.WriteLine(ht[3]); //循环输出每个值 foreach (object key in ht.Keys) Console.WriteLine(ht[key]); Console.WriteLine("------下面是HashTable集合Values案例----------------"); foreach (object value in ht.Values) Console.WriteLine(value); ht.Clear(); ht.Add("a1",new Student("a1",34)); ht.Add("ad", new Student("ad", 24)); ht.Add("sd", new Student("sd", 54)); ht.Add("fg", new Student("fg", 14)); Console.WriteLine((ht["ad"] as Student).Age); foreach (DictionaryEntry e in ht) Console.WriteLine((e.Value as Student ).Name); Console.WriteLine("------下面是List<T>集合案例----------------"); List<Student> students = new List<Student>(); students.Add(new Student("张思",34)); students.Add(new Student("张yi思", 24)); Console.WriteLine(students[0].Age); List<int> iis = new List<int>(); iis.Add(22); Dictionary<String, Student> dd = new Dictionary<string, Student>(); dd.Add("a1", new Student("a1", 33)); Class<Student> c = new Class<Student>(); c.Add(new Student("aaa", 34)); Console.WriteLine(c[0].Age); } } class Student { string name; int age; public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public Student(string name, int age) { this.name = name; this.age = age; } } class Class<X>//表示泛型,表示一种数据类型,是什么现在不知道,使用的时候去决定 { List<X> list = new List<X>(); public void Add(X x) { list.Add(x); } public X this[int index] { get { return list[index]; } } } }
看了以前的书籍,所作出的笔记,概念这嘛事,虽然有些是懂非懂,但是相信能搞定的,每次都看看,我就不相信我还能忘。。。。