C#面向对象14 List泛型集合/装箱和拆箱/字典集合(Dictionary)

1.List泛型集合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace List泛型集合
{
    class Program
    {
        static void Main(string[] args)
        {
            //
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            list.AddRange(list);

            //List泛型集合可以转换成数组
            int [] nums = list.ToArray();
            Console.WriteLine(nums.Length);
            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine(nums[i]);

            }
            /*
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }*/
            //数组转LIST泛型集合
            char[] chars = new char [] { 'a','b','c','d','e'};
            List<char> listchars = chars.ToList();
            for (int i = 0; i < listchars.Count; i++)
            {
                Console.WriteLine(listchars[i]);
            }

            Console.ReadKey();
        }
    }
}

2.装箱拆箱

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 装箱和拆箱
{
    class Program
    {
        static void Main(string[] args)
        {
            //******
            //装箱:将值类型转换为引用类型
            //拆箱:将引用类型转换为值类型
            //条件:看两种类型是否发生了装箱和拆箱,要看,这两种类型是否存在继承关系。

            int n = 10;
            object o = n;//装箱
            int nn = (int)o;//拆箱

            //装箱操作
            ArrayList list = new ArrayList();
            for (int i = 0; i < 1000000; i++)
            {
                list.Add(i);
            }

            //没有发生装箱和拆箱
            string str = "123";
            int a = Convert.ToInt32(str);

            int b =10;
            IComparable cc = b;//装箱

            Console.ReadKey();
        }
    }
}

3.字典集合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 字典集合
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "111");
            dic.Add(2, "222");
            dic.Add(3, "333");

            dic[1] = "new111";

            //foreach (var item in dic.Keys)
            //{
            //    Console.WriteLine(dic[item]);
            //}
            foreach (KeyValuePair<int,string> kv in dic)
            {

                Console.WriteLine("{0}----{1}", kv.Key, kv.Value);
            }

            Console.ReadKey();
        }
    }
}

练习一:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 集合练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //将一个数组中的奇数放到一个集合中,再将偶数放到另一个集合中
            //最终将两个集合合拼并一个集合,并且奇数显示在左边, 偶数显示在右边。
            //Convert.ToBoolean(i % 2); 1--true,0--false

            List<int> list = new List<int>();
            for (int i = 0; i < 10; i++)
            {
                list.Add(i);
            }

            List<int> list_ji = new List<int>();
            List<int> list_ou = new List<int>();

            for (int i = 0; i < list.Count; i++)
            {
                if (Convert.ToBoolean(list[i] % 2))
                {
                    list_ji.Add(list[i]);
                }
                else
                    list_ou.Add(list[i]);
            }

            Dictionary<int, int> dic = new Dictionary<int, int>();
            for (int i = 0; i < list_ji.Count; i++)
            {
                dic.Add(list_ji[i], list_ou[i]);
            }
            foreach (KeyValuePair<int,int> kv in dic)
            {
                Console.WriteLine("{0}----{1}",kv.Key,kv.Value);
            }
            

            Console.ReadKey();
        }
    }
}

练习二:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 集合练习2
{
    class Program
    {
        static void Main(string[] args)
        {
            //提示用户输入一个字符串,通过foreach循环将用户输入的字符串赋值给一个字符数组
            //string ss = "Welcome";
            //char [] chars = ss.ToCharArray();

            //统计Welcome to china 中每个字符出现的次数,不考虑大小写
            string ss = "Welcome to china";
            ss = ss.Trim();
            ss=ss.Replace(" ","");
            ss = ss.ToUpper();
            Console.WriteLine(ss);
            Dictionary<char, int> dic = new Dictionary<char, int>();
            char[] chars = ss.ToArray();
            int value=1;
            for (int i = 0; i < chars.Length; i++)
            {
                if(dic.ContainsKey(chars[i]))
                {
                    dic[chars[i]] = dic[chars[i]] + 1;
                }
                else
                {
                    dic.Add(chars[i], value);
                }
            }

            foreach (KeyValuePair<char,int> item in dic)
            {
                Console.WriteLine("{0}--{1}",item.Key,item.Value);
                
            }


            Console.ReadKey();
        }
    }
}
原文地址:https://www.cnblogs.com/youguess/p/8479742.html