zoukankan      html  css  js  c++  java
  • .Net中C# Dictionary 用法(转)

     

    Dictionary提供快速的基于键值的元素查找。
    结构是:Dictionary <[key] , [value] >,当你有很多元素的时候可以用它。
    它包含在System.Collections.Generic名控件中。在使用前,你必须声明它的键类型和值类型。
    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Demo1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //创建泛型哈希表,Key类型为int,Value类型为string
                Dictionary<int, string> myDictionary = new Dictionary<int, string>();
                //1.添加元素
                myDictionary.Add(1, "a");
                myDictionary.Add(2, "b");
                myDictionary.Add(3, "c");
                //2.删除元素
                myDictionary.Remove(3);
                //3.假如不存在元素则添加元素
                if (!myDictionary.ContainsKey(4))
                {
                    myDictionary.Add(4, "d");
                }
                //4.显示容量和元素个数
                Console.WriteLine("元素个数:{0}",myDictionary.Count);
                //5.通过key查找元素
                if (myDictionary.ContainsKey(1))
                {
                    Console.WriteLine("key:{0},value:{1}","1", myDictionary[1]);
                    Console.WriteLine(myDictionary[1]);            
                }
                //6.通过KeyValuePair遍历元素
                foreach (KeyValuePair<int,string>kvp in myDictionary)
                {
                    Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);
    
                }
                //7.得到哈希表键的集合
                Dictionary<int, string>.KeyCollection keyCol = myDictionary.Keys;
                    //遍历键的集合
                    foreach (int n in keyCol)
                    {
                        Console.WriteLine("key={0}", n);                
                    }
                //8.得到哈希表值的集合
                Dictionary<int, string>.ValueCollection valCol = myDictionary.Values;
                    //遍历值的集合
                    foreach( string s in valCol)
                    {
                    Console.WriteLine("value:{0}",s);
                    }
                //9.使用TryGetValue方法获取指定键对应的值
                string slove = string.Empty;
                if (myDictionary.TryGetValue(5, out slove))
                {
                    Console.WriteLine("查找结果:{0}", slove);
                }
                else
                {
                    Console.WriteLine("查找失败");
                }
                //10.清空哈希表
                //myDictionary.Clear();
                Console.ReadKey();
            }
        }
    }
    复制代码

    运行结果:

  • 相关阅读:
    MySQL锁
    mysql服务性能优化—my.cnf配置说明详解
    springmvc请求参数获取的几种方法
    Linux mysql 添加远程连接
    Linux 操作 mysql
    Linux 安装 mysql 转 http://www.cnblogs.com/fnlingnzb-learner/p/5830622.html
    linux 下 安装nginx
    dubbo 实战总结
    分布式事务的几种方式
    精巧好用的DelayQueue 转
  • 原文地址:https://www.cnblogs.com/yanranziruo/p/12410466.html
Copyright © 2011-2022 走看看