1 { 2 #region MyRegion 3 //定做一个箱子,装入可以装任意多个人 4 ArrayList list = new ArrayList(); 5 Tools frm = new Tools(); 6 frm.Name = "李小龙"; 7 Tools frm2 = new Tools(); 8 frm2.Name = "成龙"; 9 Tools frm3 = new Tools(); 10 frm3.Name = "李小乐"; 11 Tools frm4 = new Tools(); 12 frm4.Name = "李小乐"; 13 list.Add(frm); 14 list.Add(frm2); 15 list.Add(frm3); 16 //遍历集合中所有元素 17 foreach (Tools tool in list) 18 { 19 Console.WriteLine(tool.Name); 20 } 21 22 list.Remove(frm3); 23 //判断龙哥是否在集合中 24 if (list.Contains(frm4)) //存在 25 { 26 //list.Remove(p4); 27 //根据下标来删 28 list.RemoveAt(0); 29 } 30 else 31 { 32 Console.WriteLine("数据都不存在,如何删除???"); 33 } 34 Console.WriteLine("===============删除后========="); 35 foreach (Tools tool in list) 36 { 37 Console.WriteLine(tool.Name); 38 } 39 40 list.Insert(0, frm4); 41 Console.WriteLine("===============插入后========="); 42 foreach (Tools tool in list) 43 { 44 Console.WriteLine(tool.Name); 45 } 46 47 48 49 50 51 52 53 54 55 Console.WriteLine("*********************************"); 56 Hashtable table = new Hashtable(); 57 Person p1 = new Person(); 58 p1.Name = "张三"; 59 Person p2 = new Person(); 60 p2.Name = "赵四"; 61 Person p3 = new Person(); 62 p3.Name = "公里"; 63 table.Add("001", p1); 64 table.Add("002", p2); 65 table.Add("003", p3); 66 //遍历HashTable三种方案 67 /* 68 * 无论是java还是C#,引用类型对象都会自动调用它的ToString() 69 ToString()命名空间.类名 70 */ 71 //方式一:通过key获取key和value 72 foreach (string key in table.Keys) 73 {//通过key获取value 74 Console.WriteLine("key是{0} value是{1}", key, ((Person)table[key]).Name); 75 } 76 Console.WriteLine("**************************************"); 77 //方式二:只获取value 78 foreach (Object value in table.Values) 79 { 80 Console.WriteLine(((Person)value).Name); 81 } 82 #endregion 83 /////////////////////////////////////////////////////////////// 84 Hashtable table = new Hashtable(); 85 Person p1 = new Person(); 86 p1.Name = "张三"; 87 Person p2 = new Person(); 88 p2.Name = "赵四"; 89 Person p3 = new Person(); 90 p3.Name = "公里"; 91 table.Add("001", p1); 92 table.Add("002", p2); 93 table.Add("003", p3); 94 //遍历HashTable三种方案 95 /* 96 * 无论是java还是C#,引用类型对象都会自动调用它的ToString() 97 ToString()命名空间.类名 98 */ 99 //方式一:通过key获取key和value 100 foreach (string key in table .Keys ) 101 { 102 Console.WriteLine("key是{0} value是{1}",key ,((Person )table [key ]).Name ); 103 104 } 105 Console.WriteLine("=============================================="); 106 foreach (Object value in table .Values ) 107 { 108 Console.WriteLine(((Person )value ).Name ); 109 } 110 Console.WriteLine("==========================================="); 111 //方式三:key和value同时遍历 key+value 112 foreach (DictionaryEntry item in table) 113 { 114 Console.WriteLine("key是{0} value是{1}", item.Key, ((Person)item.Value).Name); 115 } 116 //删除 Remove() 117 table.Remove("001"); 118 //修改 咱们修改“002” key对应value值得name属性为“大美丽” 119 //先判断 是否存在某个key 120 if (table .ContainsKey("002")) 121 { 122 Person p = (Person)table["002"]; 123 p.Name = "大美丽"; 124 } 125 Console.WriteLine("===========修改后=============="); 126 //方式三:key和value同时遍历 key+value 127 foreach (DictionaryEntry item in table ) 128 { 129 Console.WriteLine("key是{0} value是{1}", item.Key, ((Person)item.Value).Name); 130 } 131 132 Console.WriteLine("==========删除后=========="); 133 //方式三:key和value同时遍历 key+value 134 foreach (DictionaryEntry item in table) 135 { 136 Console.WriteLine("key是{0} value是{1}", item.Key, ((Person)item.Value).Name); 137 } 138 Console.ReadLine(); 139 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.CodeDom; 7 using System.Collections; 8 9 namespace FrmMain 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 List<Person> list = new List<Person>(); 16 Person p1 = new Person(); 17 p1.Name = "小王"; 18 Person p2 = new Person(); 19 p2.Name = "张三"; 20 list.Add(p1); 21 list.Add(p2); 22 //foreach (Person item in list) 23 //{ 24 // Console.WriteLine(item.Name); 25 //} 26 27 list.Add(new Person() { Name="asdf",Age =12}); 28 list.Add(new Person() { Name="asdf"}); 29 list.Add(new Person() { Name="sdafsa"}); 30 foreach (Person person in list) 31 { 32 Console.WriteLine(person.Name+ person.Age); 33 } 34 35 36 37 //Dictionary<string, Person> div = new Dictionary<string, Person>(); 38 //Person p5 = new Person(); 39 //p5.Name = "李晓玲"; 40 //p5.Age = 12; 41 //Person p6 = new Person(); 42 //p6.Name = "李晓玲2"; 43 //p6.Age = 22; 44 45 //div.Add(p5.Name, p5); 46 //div.Add(p6.Name, p6); 47 48 49 50 //foreach(string item in div.Keys ) 51 //{ 52 // Console.WriteLine("key{0} {0}",item ,div [item].Name ); 53 //} 54 55 56 57 //foreach (Person value in div.Values) 58 //{ 59 // Console.WriteLine("{0} {1}", value.Name, value.Age); 60 //} 61 62 63 64 //foreach (KeyValuePair<string, Person> item in div) 65 //{ 66 // Console.WriteLine(item.Key + " " + item.Value.Age); 67 //} 68 69 //Console.ReadKey(); 70 71 72 //ArrayList list = new ArrayList() 73 //{ 74 // new Person (){Name ="李小龙",Age=12}, 75 // new Person (){Name ="李小龙2",Age=12}, 76 // new Person (){Name ="李小龙3",Age=12} 77 //}; 78 79 //for (int i = 0; i <list.Count ; i++) 80 //{ 81 // Console.WriteLine(((Person )list [i]).Name); 82 //} 83 84 Console.ReadLine(); 85 } 86 } 87 }