zoukankan      html  css  js  c++  java
  • Dictionary基本应用

    protected void Page_Load(object sender, EventArgs e)
        {
            IList<string> AList = new List<string>();
            AList.Add("KimhillZhangA");
            AList.Add("KimhillZhangB");
            AList.Add("KimhillZhangC");

            IList<string> BList = new List<string>();
            BList.Add("KimhillZhangD");
            BList.Add("KimhillZhangE");
            BList.Add("KimhillZhangF");

            Dictionary<string, IList<string>> dictionary = new Dictionary<string, IList<string>>();
            //添加键/值,方法一
            dictionary.Add("A", AList);
            //添加键/值,方法二
            dictionary["B"] = BList;

            //遍历键
            foreach (string key in dictionary.Keys)
            {
                //遍历某键的值
                foreach (string val in dictionary[key])
                {
                   
                }
            }

            //取某键的值,,使用 TryGetValue 方法作为一种更有效的方法来检索值
            if (dictionary.TryGetValue("A", out AList))
            {
                for (int i = 0; i < AList.Count; i++)
                {
                    Response.Write(AList[i]);
                }
            }
            //是否包含某键
            if (!dictionary.ContainsKey("A"))
            {
                dictionary.Add("A", AList);
            }

            //由于基于 IDictionary 的集合中的每个元素都是一个键/值对,因此元素类型既不是键的类型,也不是值的类型。而是 KeyValuePair 类型
            foreach (KeyValuePair<string, IList<string>> kvp in dictionary)
            {
                string key = kvp.Key;//key包含了两个键,A,B
                for (int i = 0; i < kvp.Value.Count; i++)
                {
                    Response.Write(kvp.Value[i]);
                }
            }

           //定义一个<string,int>的Dictionary,让它的值进行添加

          Dictionary<string,int> dic = new Dictionary<string,int>();

        //添加两个键为"成绩1","成绩2";并为它们的值赋为0

         dic["成绩1"] = 0;

         dic["成绩2"] = 0;

       // 把这两个值分别加1

         dic["成绩1"]++;

         dic["成绩2"]++;

        }

  • 相关阅读:
    [算法][求积分][复合辛普森公式]
    [51单片机] SPI nRF24L01无线 [可以放在2个单片机里实现通信]
    [51单片机] SPI nRF24L01 无线简单程序 1
    [stm32] 利用uC-BmpCvt软件生成uc-gui可调用的bmp图片
    [stm32] 利用uc-gui封装画图和画线函数移植51上的模拟动画
    [stm32] 中断
    [C++] 将 mp3 等音乐资源以资源形式嵌入 exe 文件中
    [游戏学习28] MFC 时钟
    [游戏学习27] MFC 匀速运动
    [游戏学习26] MFC 时间函数 画图形
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/1742799.html
Copyright © 2011-2022 走看看