C#集合类型

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值