S2 深入.NET平台和C#语言 第三章 课上练习
2017-02-12 14:17
585 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace 深入.NET平台第三章_课上 { class Program { /* * 自动扩容: * 如果结合中一个元素都没有的话,Capacity为0, * 如果集合中出现第一个元素,分配4个空间, * 如果出现第5个元素的时候,可以容纳8个元素, * 每次扩容会扩成上次的2倍 * * * 1.1那么,系统怎么知道何时扩容?在哪个方法中进行了扩容。 * 解析:Add方法扩容,容量不够的时候 * */ static void Main(string[] args) { int[] nums = new int[3]; ArrayList ss = new ArrayList(); ss.Add(1); ss.Add(2); ss.Add(2); ss.Add(3); ss.Add(4); foreach (Object item in list) { Console.WriteLine(item); } Console.WriteLine(ss.Capacity + " 容量"); // 6 Console.WriteLine(ss.Count + " 真正存储元素个数");//5 ArrayList engineers = new ArrayList(){ new student(){ Name="王",Author="hh",Price=20 }, new student(){ Name="王高达",Author="苏苏",Price=40 }, new student(){ Name="王地方",Author="恩恩",Price=390 }, }; Console.WriteLine(engineers.Count.ToString()); // ForeachTest(); student student1 = new student("如果蜗牛有爱情", "丁墨", 50); student student2 = new student("花千骨", "果儿", 60); student student3 = new student("盗墓笔记", "南派三叔", 70); list.Add(student1); list.Add(student2); list.Add(student3); foreach (student item in list) { Console.WriteLine(item.Name); } Console.WriteLine("===================修改==============="); foreach (student item in list) { if (item.Name.Equals("如果蜗牛有爱情")) { item.Name = "他来了,请闭眼"; break; } } foreach (student item in list) { Console.WriteLine(item.Name); } //按索引删除 Console.WriteLine("===================================我是高贵的分界线================"); list.RemoveAt(0); foreach (student item in list) { Console.WriteLine(item.Name); } //按对象删除 list.Remove("花千骨"); Console.WriteLine("===================================我是高贵的分界线================"); foreach (student item in list) { Console.WriteLine(item.Name); } //清空所有 Console.WriteLine("===================================我是高贵的分界线================"); list.Clear(); foreach (student item in list) { Console.WriteLine(item.Name); } //对象初始化器 student stu = new student() { Name = "苏琳琳" }; ArrayList l = new ArrayList() { stu }; //双列集合 Hashtable table = new Hashtable(); table.Add("CBC", "建设银行"); table.Add("ABC", "中国农业银行"); table.Add("ICBC", "工商银行"); //方式一:遍历 根据key拿到 VaLue,Next key的 集合 foreach (string key in table.Keys) { Console.WriteLine("key是:"+key); //value 如何通过key 拿到 value! //索引器 object value = table[key]; Console.WriteLine("value是:"+value.ToString()); } Console.WriteLine("================================================="); //方式二:直接遍历value foreach (string value in table.Values) { Console.WriteLine(value); } //03 key和value一次遍历一个项目===(key+value) foreach (DictionaryEntry item in table) { Console.WriteLine(item.Key+" "+item.Value); } Console.ReadLine(); } static ArrayList list = new ArrayList(); public static void ForeachTest() { student student1 = new student("如果蜗牛有爱情", "丁墨", 50); student student2 = new student("花千骨", "果儿", 60); student student3 = new student("盗墓笔记", "南派三叔", 70); list.Add(student1); list.Add(student2); list.Add(student3); foreach(student item in list){ Console.WriteLine(item.Name); } } } }
相关文章推荐
- 深入.NET平台和C#语言 第三章员工打卡
- 深入.NET平台和C#编程 第三章 学习笔记
- 深入.NET平台和C#编程第二张上机练习2
- 深入.NET平台和C#编程 第二章 学习笔记
- (1-2)章深入.NET平台和C#编程 复习的时候 整理的笔记
- 深入.NET和c#理解---第三章技能知识点(ArrayList/Hashtable/泛型集合)
- 深入.NET平台和C#编程_使用集合组织相关数据
- 深入.NET平台和C#编程_深入C#数据类型
- 深入.NET平台和C#编程 第二章 上机3 经理评分
- 深入。NET平台和C#编程 第二章课后练习 购物 + 加分
- 《C#和.NET2.0实战——平台、语言与框架》
- 深入.NET平台和C#编程 第五章 学习笔记
- .NET学习笔记(3)——深入.net平台和C#编程
- 深入.NET平台和C#编程 第四章 学习笔记
- C#和.NET2.0实战 平台、语言与框架 PDF下载
- ACCP5.0 深入.NET平台和C#编程 小总结
- 深入.NET平台和C#编程 错题录
- 那些年【深入.NET平台和C#编程】
- [好书推荐] C#和.NET 2.0实战:平台、语言与框架
- ACCP7.0S2深入.net和c#第6章上机练习