List泛型集合、装箱和拆箱、Dictionary

1、List泛型集合

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _02_List泛型集合
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //创建泛型集合对象
14             //List<int> list = new List<int>();
15             //list.Add(1);
16             //list.Add(2);
17             //list.Add(3);
18 
19             //list.AddRange(new int[] { 1, 2, 3, 4, 5, 6 });
20             //list.AddRange(list);
21 
22             //List泛型集合可以转换为数组
23             //int[] nums = list.ToArray();
24 
25             //List<string> listStr = new List<string>();
26 
27             //string[] str = listStr.ToArray();
28 
29 
30             //char[] chs = new char[] { 'c', 'b', 'a' };
31             //List<char> listChar = chs.ToList();
32             //for (int i = 0; i < listChar.Count; i++)
33             //{
34             //    Console.WriteLine(listChar[i]);
35             //}
36 
37             ////   List<int> listTwo = nums.ToList();
38 
39 
40             //for (int i = 0; i < list.Count; i++)
41             //{
42             //    Console.WriteLine(list[i]);
43             //}
44             Console.ReadKey();
45         }
46     }
47 }
View Code

 

2、装箱和拆箱

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Diagnostics;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace _03_装箱和拆箱
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             //int n = 10;
16             //object o = n;//装箱
17             //int nn = (int)o;//拆箱
18 
19 
20             //ArrayList list = new ArrayList();
21             //List<int> list = new List<int>();
22             ////这个循环发生了100万次装箱操作
23             //Stopwatch sw = new Stopwatch();
24             ////00:00:02.4370587
25             ////00:00:00.2857600
26             //sw.Start();
27             //for (int i = 0; i < 10000000; i++)
28             //{
29             //    list.Add(i);
30             //}
31             //sw.Stop();
32             //Console.WriteLine(sw.Elapsed);
33             //Console.ReadKey();
34 
35 
36             //这个地方没有发生任意类型的装箱或者拆箱
37             //string str = "123";
38             //int n = Convert.ToInt32(str);
39 
40 
41             int n = 10;
42             IComparable i = n;//装箱
43 
44             //发生
45         }
46     }
47 }
View Code

 

3、Dictionary

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace _04Dictionary
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Dictionary<int, string> dic = new Dictionary<int, string>();
14             dic.Add(1, "张三");
15             dic.Add(2, "李四");
16             dic.Add(3, "王五");
17             dic[1] = "新来的";
18             foreach (KeyValuePair<int,string> kv in dic)
19             {
20                 Console.WriteLine("{0}---{1}",kv.Key,kv.Value);
21             }
22 
23             //foreach (var item in dic.Keys)
24             //{
25             //    Console.WriteLine("{0}---{1}",item,dic[item]);
26             //}
27             Console.ReadKey();
28         }
29     }
30 }
View Code


4、泛型集合的练习

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

 

转载于:https://www.cnblogs.com/liuslayer/p/4475493.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值