今天在云和学院复习了以前的知识,又学习了ArrayList和HashTable
ArrayList实例:
ArrayList arr = new ArrayList(); int i = 123; string str = "宋连城"; float f = 1.23f; decimal d = 2.13m; double dou = 22.16; arr.Add(i); arr.Add(str); arr.Add(f); arr.Add(d); arr.Add(dou); arr.Remove(dou); arr.RemoveAt(2); for (int j = 0; j < arr.Count; j++) { Console.WriteLine(arr[j]); } Console.ReadKey();
HashTable实例:
Hashtable hash = new Hashtable(); string id = "272012321029"; string name = "宋丽"; string id1 = "272012321028"; string name1 = "婷婷"; hash.Add(id,name); hash.Add(id1,name1); foreach (DictionaryEntry item in hash) { Console.WriteLine(item.Key); Console.WriteLine(item.Value); } int[] array = {12,45,23,89 }; foreach (var item in array) { Console.WriteLine(item); } Console.ReadKey();
foreach语法:
foreach(集合中单个的类型 局部变量名 in 集合对象)
{
–// 循环体
–// 循环体当中“局部变量”表示集合中遍历的数据
}
泛型集合
List<int> list = new List<int>(); for (int i = 1; i < 100; i++) { list.Add(i); } foreach (var item in list) { Console.WriteLine(item); } Console.ReadKey();
Dictionary
Dictionary <TKey,TValue>
Dictionary实例:
Dictionary<int, string> dic = new Dictionary<int, string>(); dic.Add(1,"范冰冰"); dic.Add(2,"周韦彤"); dic.Add(3,"范玮琪"); dic.Add(4,"蔡国庆"); dic.Add(5, "孙俪"); foreach (var item in dic) { Console.WriteLine(item.Key); Console.WriteLine(item.Value); } Console.ReadKey();
时间: 2024-12-16 18:52:17