• 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
  • 相关阅读:
    利用BitLocker和vhdx创建一个有加密的Win10系统
    macOS 10.12 任何来源
    Xcode 8 GM 编译缺失 /Users/usr/lib/libresolv.9.dylib
    基于inline-block的列表布局
    markdown 的基本操作
    easyui1.32 各种问题汇总
    angular笔记
    underscore 笔记
    我的问道游戏主题皮肤
    在bootstrap ace样式框架上修改的后台管理型模板(Tab页后台管理模板)
  • 原文地址:https://www.cnblogs.com/liuslayer/p/4475493.html
Copyright © 2020-2023  润新知