C#集合类型

来源:互联网 发布:淘宝卖家后台功能介绍 编辑:程序博客网 时间:2024/05/27 20:50

1. 数组:

namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {            //数组;继承于抽象类System.Array            int[] number = new int[5];            //二维数组:            string[,] name = new string[5, 4];            //数组的数组,每一行长度可以不同            byte[][] scores = new byte[5][];            for (int i=0; i<scores.Length; i++)            {                scores[i] = new byte[i + 3];            }            for (int i = 0; i < scores.Length; i++)            {                Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);            }            Console.ReadLine();        }    }}


数组的初始化方式:

            //数组初始化            int[] number1 = new int[5] { 1,2,3,4,5};            //或者            int[] number2 = new int[] { 1, 2, 3, 4, 5 };            //或者            int[] number3 = { 1, 2, 3, 4, 5 };            //多维数组初始化            string[,] name1 = { { "a", "k" }, { "h", "j" } };            //数组的数组            int[][] scores1 = { new int[]{ 1,2,3}, new int[]{ 3,4,5,6} };            //数组中成员的获取:            Console.WriteLine(number1[2]);            //数组中的方法:            Console.WriteLine(number1.Length);            //所有成员的获取:            foreach(int i in number1)            {                Console.WriteLine(i);            }

注意:数组计数从0开始



2.  ArrayList 和 List

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3{    class Program    {        static void Main(string[] args)        {            //1. ArrayList : 继承于System.Collection,数组列表可以存储各种类型            ArrayList al = new ArrayList();            al.Add(5);            al.Add(100);            al.Remove(5);            al.Add("thystar");            foreach(var e in al)            {                Console.WriteLine(e);            }            //用数组列表访问成员            Console.WriteLine(al[0]);            //2.  List : 只能存储一种类型            List<int> intList = new List<int>();            intList.Add(3);            intList.AddRange(new int[] { 5, 3, 2 });            //List中是否包含某个值:            Console.WriteLine(intList.Contains(3));            //List中某个数值的位置:            Console.WriteLine(intList.IndexOf(5));            //删除            intList.Remove(5);            //在中间某个位置插入            intList.Insert(1, 222);            Console.ReadLine();        }    }}

3. Hashtable 和 Dictionary, 以及其他一些集合类型:

            // 哈希表:key : value形式, 对类型没有强制规定            Hashtable ht = new Hashtable();            ht.Add("1", "first");            ht.Add("2", "second");            Console.WriteLine(ht["1"]);            //哈希表访问无效key值时返回空,不会报错            //用Dictionary替代哈希表,强制规定数据类型:            Dictionary<string, string> d = new Dictionary<string, string>();            d.Add("first", "hello");            d.Add("second", "world");            Console.WriteLine(d["first"]);            //Dictionary访问无效key时会报错;因此,它比哈希表更安全;            //但是,Dictionary不是一个线程安全的类型:            // 线程安全的类型用ConcurrentDictionary            // SortedList: 经过排序的List, 通过key的值排序            SortedList<int, int> sl = new SortedList<int, int>();            sl.Add(3, 4);            sl.Add(2, 5);            sl.Add(4, 6);            sl.Add(1, 100);            foreach(var sle in sl)            {                Console.WriteLine(sle);                Console.WriteLine(sle.Value);            }            // stack 栈类型:先进后出            // queue 队列: 先进先出







0 0