1. using System;
    2. using System.Collections.Generic;
    3. namespace _077_泛型集合的练习
    4. {
    5. class Program
    6. {
    7. static void Main(string[] args)
    8. {
    9. //1、将一个数组的奇数放到一个集合中,再将偶数放到另一个集合中
    10. // 最终将两个集合合并成一个集合,并且奇数显示在左边,偶数显示在右边。
    11. int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    12. List<int> listOdd = new List<int>();
    13. List<int> listEven = new List<int>();
    14. for (int i = 0; i < nums.Length; i++)
    15. {
    16. if (nums[i] % 2 == 0)
    17. {
    18. listEven.Add(nums[i]);
    19. }
    20. else
    21. {
    22. listOdd.Add(nums[i]);
    23. }
    24. }
    25. List<int> list = new List<int>();
    26. list.AddRange(listOdd);
    27. list.AddRange(listEven);
    28. //foreach (var item in listOdd)
    29. //{
    30. // list.Add(item);
    31. //}
    32. //foreach (var item in listEven)
    33. //{
    34. // list.Add(item);
    35. //}
    36. foreach (var item in list)
    37. {
    38. Console.Write(item + " ");
    39. }
    40. Console.WriteLine();
    41. //2、提示用户输入一个字符串,通过foreach循环将用户输入的字符串赋值给一个字符数组。
    42. //Console.WriteLine("请输入数据:");
    43. //string input = Console.ReadLine();
    44. //char[] chs = new char[input.Length];
    45. //int j = 0;
    46. //foreach (var item in input)
    47. //{
    48. // chs[j] = item;
    49. // j++;
    50. //}
    51. ////List<char> listChar = new List<char>();
    52. ////foreach (char item in input)
    53. ////{
    54. //// listChar.Add(item);
    55. ////}
    56. ////foreach (var item in listChar)
    57. ////{
    58. //// Console.WriteLine(item);
    59. ////}
    60. //foreach (var item in chs)
    61. //{
    62. // Console.WriteLine(item);
    63. //}
    64. //3、统计Welcome to China中每个字符出现的次数,不考虑大小写
    65. string s = "Welcome to China";
    66. s = s.ToLower();
    67. //字符作为键 次数为值
    68. Dictionary<char, int> dic = new Dictionary<char, int>();
    69. for (int i = 0; i < s.Length; i++)
    70. {
    71. if (s[i] == ' ')
    72. {
    73. continue;
    74. }
    75. //如果dic已经包含了当前循环到的这个键
    76. if (dic.ContainsKey(s[i]))
    77. {
    78. //值再次+1
    79. dic[s[i]]++;
    80. }
    81. else//这个字符在集合当中是第一次出现
    82. {
    83. //dic.Add(s[i], 1);
    84. dic[s[i]] = 1;
    85. }
    86. }
    87. //foreach (var item in dic)
    88. //{
    89. // Console.WriteLine(item);
    90. //}
    91. foreach (KeyValuePair<char, int> item in dic)
    92. {
    93. Console.WriteLine("{0}出现了{1}次", item.Key, item.Value);
    94. }
    95. Console.ReadKey();
    96. }
    97. }
    98. }