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

        }

  • 相关阅读:
    kube-apiserver
    深度学习三:卷积神经网络
    深度学习二:概率和反向传播的变种
    深度学习一:深度前馈网络和反向传播
    Knowledge 1:Propositional Logic 命题逻辑基础及符号
    评估方法:留出法、交叉验证法、自助法、调参与最终模型
    你曾这样问过
    套路总结
    NOI2020游记
    curl不是内部或外部命令,也不是可运行的程序或批处理文件
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/1742799.html
Copyright © 2011-2022 走看看