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

        }

  • 相关阅读:
    从零开始学android -- dialog
    Java学习笔记之equals和Objects.equals
    windows svchost.exe 引起的出现的莫名其妙的窗口失去焦点
    android.util.AndroidRuntimeException Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? com.uethinking.microvideo.manag
    从零开始搭建android框架系列(转)
    js-ES6学习笔记-编程风格(1)
    js-ES6学习笔记-module(4)
    js-ES6学习笔记-module(3)
    js-ES6学习笔记-module(2)
    js-ES6学习笔记-module(1)
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/1742799.html
Copyright © 2011-2022 走看看