6 1 1 Array List 2 Hashtable 3
知识架构 返回目录 6. 1 集合 1 Array. List类 2 Hashtable类 3 在Visual Studio中自定义开发设置 Stack类 43 在Visual Studio中自定义开发设置 Queue类
✎ Array. List类 表 6 -2 Array. List类常用方法
✎ Array. List类 3. 创建动态数组 创建方法: Array. List 列表对象名=new Array. List( ); 例如: Array. List my. List = new Array. List(); my. List. Add("Hello"); my. List. Add("World");
✎ Array. List类 4. Array. List类使用实例 【例6 -1】 使用Array. List类,实现列表中元素的添加、插入和删除,并将列表遍历输出。 class Program {static void Main(string[] args){ Array. List my. List = new Array. List(); //创建列标对象my. List. Add("Visual"); //列表中添加元素 my. List. Add("C#"); my. List. Add("2010"); my. List. Insert(0, "Welcome"); //在列标的起始位置插入一个元素 my. List. Insert(1, "to"); Print. Values(my. List); Console. Write. Line("删除后:"); my. List. Remove("Welcome"); //删除列表中的元素“Welcome” my. List. Remove. At(0); //删除下表为 0的元素 Print. Values(my. List); }
✎ Array. List类 4. Array. List类使用实例 【例6 -1】 使用Array. List类,实现列表中元素的添加、插入和删除,并将列表遍历输出。 class Program {static void Main(string[] args){ Array. List my. List = new Array. List(); //创建列标对象my. List. Add("Visual"); //列表中添加元素 my. List. Add("C#"); my. List. Add("2010"); my. List. Insert(0, "Welcome"); //在列标的起始位置插入一个元素 my. List. Insert(1, "to"); Print. Values(my. List); Console. Write. Line("删除后:"); my. List. Remove("Welcome"); //删除列表中的元素“Welcome” my. List. Remove. At(0); //删除下表为 0的元素 Print. Values(my. List); }
✎ Array. List类 public static void Print. Values(Array. List 1) { foreach (Object obj in List 1) Console. Write("{0}t", obj); Console. Write. Line(); } }
✎ Hashtable类 表 6 -4 Hashtable类常用的方法
✎ Stack类 【例6 -3】 使用不同的方法创建栈,并对栈进行遍历、进栈、出栈、读栈顶元素的操作。 static void Main(string[] args){ Stack my. Stack = new Stack(); //创建空栈 //元素 1, 2, 3, 4依次进栈 my. Stack. Push(1); my. Stack. Push(2); my. Stack. Push(3); my. Stack. Push(4); foreach(int i in my. Stack ) //遍历栈中的元素 Console. Write. Line("{0}t", i); Console. Write. Line("栈顶:{0}", my. Stack. Peek()); //读出栈顶元素 my. Stack. Pop(); //删除栈顶元素 Console. Write. Line("当前栈顶:{0}", my. Stack. Peek()); //读出当前栈顶元素 Stack st = new Stack(my. Stack); //创建实例st,并将元素复制到st中 foreach (int i in st) Console. Write. Line("{0}t", i); }
✎ Queue类 3. Queue对象的创建和Stack对象的创建非常相似。常用以下方法: Queue 队列对象名=new Queue(); Queue 队列对象名=new Queue(ICollection col); Queue 队列对象名=new Queue(int capacity);
✎ Queue类 【例6 -4】 创建队列,并对队列进行插入元素、删除元素和遍历的操作。 static void Main(string[] args) { Queue my. Que = new Queue(); //创建队列 Console. Write. Line("队列:"); my. Que. Enqueue("one"); //将元素插入到对头 my. Que. Enqueue("two"); my. Que. Enqueue("three"); my. Que. Enqueue("four"); foreach (string st in my. Que) //遍历队列 Console. Write. Line("{0}t", st); my. Que. Dequeue(); //删除队尾元素 Console. Write. Line ("删除元素后的队列:"); foreach (string st in my. Que) Console. Write. Line("{0}t", st); }
✎ List<T>类 【例6 -5】 使用List<T>类,实现不同类型列表的插入、删除和遍历操作。 static void Main(string[] args) { List <string> my. List=new List<string>(); //定义泛型类的实例 my. List. Add ("Welcome"); //添加元素 my. List. Add ("to"); my. List. Add ("Bei. Jing"); my. List. Remove("Bei. Jing"); //删除元素 foreach (string st in my. List) //遍历元素 Console. Write. Line("{0}t", st); int[] a=new int[5]{1, 2, 3, 4, 5}; List<int> list 1 = new List<int>(); //定义泛型类的实例 list 1. Add. Range(a); //将数组中的元素添加到list 1中 foreach (int st in list 1) Console. Write. Line(st); }
✎ Dictionary<K, V>类 【例6 -6】 使用Dictionary<K, V>泛型类定义学生集合,并实现添加、删除、遍历、查找 操作。 static void Main(string[] args) { //创建Dictionary泛型类实例student Dictionary<int, string> student = new Dictionary<int, string>(); student. Add(1001, "李明"); //添加键/值对元素 student. Add(1002, "王丽丽"); student. Add(1003, "刘大伟"); student. Add(1004, "张强"); foreach (Key. Value. Pair<int, string> st in student) //遍历键/值对 Console. Write. Line("{0}t{1}", st. Key, st. Value); student. Remove(1003); //删除学号 Find. Student(student, 1003); Find. Student(student, 1004); }
- Slides: 28