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"]++;

        }

  • 相关阅读:
    mysql 数据库的简单操作 2
    mysql简单操作,增删查改.
    当mysq启动时出现错误1067时应如何解决
    JS中 逻辑或 || 逻辑与 && 的使用方法总结
    编写一段程序,运行时向用户提问“你考了多少分?(0~100)”,接受输入后判断其等级并显示出来。判断依据如下:等级={优 (90~100分);良 (80~89分);中 (60~69分);差 (0~59分);}
    IF的使用
    第一个输出程序 Console.WriteLine
    Day2_and_Day3 文件操作
    linux安装VLAN,系统怎么划分VLAN打标签上交换机
    Python3.5+selenium(11)脚本模块化&参数化
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/1742799.html
Copyright © 2011-2022 走看看