[关闭]
@callofduty890 2022-05-10T09:13:51.000000Z 字数 6462 阅读 916

【Arraylist/List】/[Hashtable/Dictionary]-练习题

编程作业

ArrayList集合类型的基本操作

使用ArrayList类操作数组列表。
(1)分别创建并初始化源字符串数组列表ArrayList({”0”,”2”,”4”,”6”})和目标字符串数组列表ArrayList({”1”,”3”,”5”,”7”,”9”})。
(2)测试CopyTo的功能,将源ArrayList的第二个元素内容复制到目标ArraylList索引为3的位置。
(3)源数组列表ArravList重新赋值为整数类型型({0,2,4。6。8 ,10 ,12} )。
(4),测试Remove的功能,先移除ArrayList中值为4的元素,再移除索引为4处的元素。
(5),测试Insert的功能,将元素400插入到ArrayList中、索引为2的位置。
(6)遍历数组列表ArrayList中的所有元素。

image_1e8bf73qcm2m1c3v1a6v1eiv10tk9.png-31.2kB

  1. static void Main(string[] args)
  2. {
  3. //初始化并装进一个新的字符串数组列表
  4. ArrayList myAL = new ArrayList();
  5. myAL.Add("0"); myAL.Add("2"); myAL.Add("4"); myAL.Add("6");
  6. //myTargetArray数组
  7. string[] myTargetArray = new string[5];
  8. myTargetArray[0] = "1";
  9. myTargetArray[1] = "3";
  10. myTargetArray[2] = "5";
  11. myTargetArray[3] = "7";
  12. myTargetArray[4] = "9";
  13. //显示myAL
  14. Console.WriteLine("源myAL内容如下(Copying)之前");
  15. PrinValues(myAL, " ");
  16. //显示myTargetArray
  17. Console.WriteLine("源myTargetArray内容如下(Copying)之前");
  18. PrinValues(myTargetArray, " ");
  19. //执行CopyTo
  20. myAL.CopyTo(1, myTargetArray, 3, 1);
  21. Console.WriteLine("源myTargetArray执行copyTo之后");
  22. PrinValues(myTargetArray, " ");
  23. //清空数组
  24. myAL.Clear();
  25. //重新赋值
  26. myAL.Add("0");
  27. myAL.Add("2");
  28. myAL.Add("4");
  29. myAL.Add("6");
  30. myAL.Add("8");
  31. myAL.Add("10");
  32. myAL.Add("12");
  33. Console.WriteLine("myAL 重新赋值之后");
  34. PrinValues(myAL, " ");
  35. //移除某些元素
  36. myAL.Remove("4");//移除字符串为"4"
  37. myAL.RemoveAt(4);//移除索引为4的值
  38. Console.WriteLine("myAL 移除某些元素之后");
  39. PrinValues(myAL, " ");
  40. //插入指定元素
  41. myAL.Insert(2, 400);
  42. Console.WriteLine("myAl 插入某些元素之后");
  43. PrinValues(myAL, " ");
  44. Console.ReadKey();
  45. }
  46. /// <summary>
  47. /// 遍历输出内容
  48. /// </summary>
  49. /// <param name="myList">列表内容</param>
  50. /// <param name="mySqparasrtor">间隔字符串</param>
  51. public static void PrinValues(IEnumerable myList, string mySqparasrtor)
  52. {
  53. foreach (var item in myList)
  54. {
  55. Console.Write("{0} {1}", mySqparasrtor, item);
  56. }
  57. Console.WriteLine();//换行方便下一轮的输出
  58. }

List 泛型集合类型的基本操作

使用List泛型类操作整数列表。
(1)创建一个空的List< int>,并使用Add方法添加0一8中的偶数。
(2)测试List< int>的Count属性和Capacity属性。
(3)使用Contains方法测试元素6是否存在。
(4)测试Insert的功能,将元素4插入到List< int>中的指定索引2处
(5)使用Item属性(在C#中为索引器)检索List< int>中索引为3的元素。
(6)测试Remove的功能,删除List< int>中的元素4。
(7)遍历List< int>中的所有元素。
image_1e8bfmfl01d1eld0m01hged9jm.png-36.1kB

  1. static void Main(string[] args)
  2. {
  3. //初始化List
  4. List<int> evenNums = new List<int>();
  5. Console.WriteLine("初始化List<int>类的新实例化之后,Capacity={0},Count={1}", evenNums.Capacity, evenNums.Count);
  6. //添加偶数
  7. for (int i = 0; i < 4; i++)
  8. {
  9. evenNums.Add(i * 2);
  10. }
  11. Console.WriteLine("对象添加到List之后,List的内容如下");
  12. foreach (var item in evenNums)
  13. {
  14. Console.Write(" " + item);
  15. }
  16. Console.WriteLine("\n将对象添加到List之后,Capacity={0},Count={1}", evenNums.Capacity, evenNums.Count);
  17. Console.WriteLine("List是否包含6: \n {0}", evenNums.Contains(6));
  18. Console.WriteLine("List索引值2处插入4,并显示List内容:");
  19. evenNums.Insert(2, 4);
  20. foreach (var item in evenNums)
  21. {
  22. Console.Write(" " + item);
  23. }
  24. Console.WriteLine("\n evenNums[3]={0}", evenNums[3]);
  25. Console.WriteLine("从List中删除4,并显示List内容");
  26. evenNums.Remove(4);//删除4
  27. //遍历输出内容
  28. foreach (var item in evenNums)
  29. {
  30. Console.Write(" " + item);
  31. }
  32. Console.ReadKey();
  33. }

Hashtable 集合类型的基本操作

(1)创建一个空的Hashtable,并使用Add方法添加一些元素。在尝试添加重复的键时,Add方法将引发ArgumentException。
(2)使用Item属性(在C#中为索引器)来检索、设置、替换值。
(3)在调用Add方法添加元素之前,使用ContainsKey方法测试键是否存在。
(4)使用Remove肯法刪除key/value对。
(5)使用两种方法枚举字典中的键和值。
①方法一:利用foreach枚举Hashtable中每个元素。
②方法二:使用Keys属性获取并枚举Hashtable的所有keys清单;使用Values属性获取并枚举Hashtable的所有Values清单。
image_1e8bftca05uch7t7tg1k4q3ah13.png-63.8kB

  1. static void Main(string[] args)
  2. {
  3. //创建哈希表
  4. Hashtable openwith = new Hashtable();
  5. //添加元素
  6. openwith.Add("txt", "记事本.exe");
  7. openwith.Add("bmp", "图片查看器.exe");
  8. openwith.Add("sln", "vs2019.exe");
  9. openwith.Add("doc", "office.exe");
  10. //显示HashTable的内容
  11. Console.WriteLine("openwith 初始化的内容");
  12. PrinKeyAndValues1(openwith);
  13. //判断是是否存在一个key值,使用add方法
  14. Console.WriteLine("Add(\"txt\",\"记事本.exe\")");
  15. try
  16. {
  17. openwith.Add("txt","记事本.exe");
  18. }
  19. catch
  20. {
  21. Console.WriteLine("键 Key=\"txt\" 已存在!");
  22. }
  23. //Hashtable的Item属性是默认值,可以利用Key获取value
  24. Console.WriteLine("\nKey=\"doc\",value={0},openwith[\"doc\"]", openwith["doc"]);
  25. //利用item修改
  26. Console.WriteLine("\n利用openWith[\"doc\"]=office.exe 更改值.");
  27. openwith["doc"] = "WPS.exe";
  28. Console.WriteLine("使用Add(\"lxml\",\"IE.exe\")");
  29. //查找元素
  30. if (!openwith.ContainsKey("lxml"))
  31. {
  32. openwith.Add("lxml", "IE.exe");
  33. }
  34. else
  35. {
  36. Console.WriteLine("已经存在无法添加");
  37. }
  38. //显示内容
  39. Console.WriteLine("\nopenwith 显示内容");
  40. PrinKeyAndValues2(openwith);
  41. //利用Remove方法删除Key/Value
  42. openwith.Remove("bmp");
  43. //判断bmp是否被成功删除
  44. if (!openwith.Contains("bmp"))
  45. {
  46. Console.WriteLine("\"Key\"=\"bmp\" 已成功删除! ");
  47. }
  48. //显示全部内容
  49. Console.WriteLine("\n HashTable最终的内容:");
  50. PrinKeyAndValues2(openwith);
  51. Console.ReadKey();
  52. }
  53. public static void PrinKeyAndValues1(Hashtable myTable)
  54. {
  55. foreach (var item in myTable.Keys)
  56. {
  57. Console.WriteLine("Key={0},Value={1}", item, myTable[item]);
  58. }
  59. Console.WriteLine();
  60. }
  61. public static void PrinKeyAndValues2(Hashtable myTable)
  62. {
  63. foreach (DictionaryEntry item in myTable)
  64. {
  65. Console.WriteLine("Key={0},Value={1}", item.Key, item.Value);
  66. }
  67. Console.WriteLine();
  68. }

Dictionary泛型集合类型的基本操作

:使用Dictionary泛型类操作数据集合。
(1),创建一个空的带有字符串键的字符串Dictionary< TKey,TValue>,并使用Add方法添加一些元素。在添加重复的键时,Add方法将引发ArgumentException。
(2)使用Item属性来更改、设置、检索值。
(3)使用TryGetValue方法检索词典中的键值。
(4)在调用Add方法之前使用ContainsKey方法测试键是否存在。
(5)使用Remove方法删除key/value对。
(6)使用两种方法枚举字典中的键和值。
①方法一:利用foreach枚举Dictionary中每个元素。
②方法二:使用Keys属性获取并枚举Dictionary的所有Keys清单;使用Values属性获取并枚举Dictionary的所有Values清单。

image_1e8bg3fkgrh4117m11018o21epd1g.png-31.4kB

  1. static void Main(string[] args)
  2. {
  3. //创建一个新的 Dictionary<string, string>
  4. Dictionary<string, string> openWith = new Dictionary<string, string>();
  5. //Dictionary 中增加元素,注意:key不许不相同,value可以相同
  6. openWith.Add("bmp", "图片查看器.exe");
  7. openWith.Add("txt", "Notepad.exe");
  8. openWith.Add("doc", "office.exe");
  9. //显示Dictionary初始内容
  10. Console.WriteLine("openWith初始的内容");
  11. PrintKeysAndValues1(openWith);
  12. //利用Dictionary[key]获取value
  13. Console.WriteLine("\nkey=\"doc\",value={0} ", openWith["doc"]);
  14. //利用TryGetValue方法获取与指定的键相关联的值
  15. string value = "";
  16. if (openWith.TryGetValue("bmp", out value))
  17. {
  18. Console.WriteLine("key=\"bmp\",value={0}. ", value);
  19. }
  20. else
  21. {
  22. Console.WriteLine("Key=\"bmp\" is not found ");
  23. }
  24. //利用ContainsKey方法判断Key是否存在
  25. Console.WriteLine("利用ContainsKey判断key是否存在");
  26. Console.WriteLine();
  27. if (!openWith.ContainsKey("XML"))
  28. {
  29. openWith.Add("xml", "IE.exe");
  30. }
  31. //利用Remve方法删除 key/values对
  32. Console.WriteLine("利用Remove方法删除(\"bmp\"):");
  33. openWith.Remove("bmp");
  34. //显示Dictionary的内容
  35. PrintKeysAndValues1(openWith);
  36. //
  37. Console.ReadLine();
  38. }
  39. //使用两种方法枚举字典中的键和值
  40. public static void PrintKeysAndValues1(Dictionary<string, string> myDictionary)
  41. {
  42. //利用foreach枚举Dictionary中的每一个元素
  43. foreach (KeyValuePair<string, string> KeyValue in myDictionary)
  44. {
  45. Console.WriteLine(" Key={0}, Value={1}", KeyValue.Key, KeyValue.Value);
  46. }
  47. }
  48. public static void PrintKeysAndValues2(Dictionary<string, string> myDictionary)
  49. {
  50. //使用Keys属性,获取dictionary的所有Keys清单
  51. Dictionary<string, string>.KeyCollection MyKeys = myDictionary.Keys;
  52. Console.WriteLine("Dictionary所有Key的值:");
  53. foreach (string item in MyKeys)
  54. {
  55. Console.WriteLine(" Key = {0}", item);
  56. }
  57. //使用Values属性,获取Dictionary所有Values清单
  58. Dictionary<string, string>.ValueCollection MyValues = myDictionary.Values;
  59. Console.WriteLine("Dictionary所有Values的值:");
  60. foreach (string item in MyValues)
  61. {
  62. Console.WriteLine(" Values = {0}", item);
  63. }
  64. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注