Dictionary集合运用
Dictionary基础定义:
- 从一组键(key)到一组值(value)的映射,每一个添加项都是由一个值及其相关联的键组成;
- 任何键都必须是唯一的;
- 键不能为空引用的null(VB中的Nothing),若值为引用类型,则可以为空;
- key 和value可以是任何类型(int,string,bool等);
Demo:key为int类型,值为string类型
初始化
Dictionary<int,string> dic=new Dictionary<int,string>();
添加元素
dic.Add(1,"C#");
dic.Add(2,"Java");
通过key查找元素
if(dic.ContainsKey(1))
{
key(主键)=1;
value(数据列)=dic[1];
}
通过keyValuePair遍历元素
foreach(keyValuePair<int,string>kvp in dic)
{
Console.WriteLine("key={0},value{1}",kvp.key,key.value);
key(主键)=kvp.key;
key(数据列)=kvp.value;
}
近遍历键keys属性
Dictionary<int,string>.keyConllection keycol=dic.keys;
foreach(int key in keycol)
{
key(主键)=key;
}
仅遍历values属性
Dictionary<int,string>.ValueConllection valcol=dic.values;
foreach(string value in valcol)
{
value(数据列值)=value;
}
通过Remove方法移除指定的键值
dic.Remove(1);
if(dic.ContainsKey(1))
{
key=1;
value=key[1]
}
else {
Console.WriteLine("不存在key:1");
}
Dictionary常用属性和方法:
- Comparer:获取用于确定字典中的键是否相等的IEqualityComparer;
- Count:获取包含在Dictionary中的键/值对的总数;
- Item:获取或设置与指定的键相关联的值;
- keys:获取包含Dictionary中键的集合;
- values:获取包含Dictionary中值的集合;
- Add:将指定的键和值添加到字典中;
- Clear:从Dictionary中移除所有的键和值;
- ContainsKey:确定Dictionary是否包含指定的键;
- ContainsValue:确定Dictionary是否包含特定值;
- GetEnumerator:返回循环访问Dictionary的枚举数;
- GetType:获取当前实例的type(从object继承);
- Remove:从Dictionary中移除所指定的键的值;
- Tostring:返回表示当前Object的string(从object继承);
- TryGetValue:获取与指定的键相关联的值;

Dictionary集合运用的更多相关文章
- C#Dictionary集合的使用
题目:输入一串字符串字母,比如:Welcome to China,比较每个字母出现的次数,不区分大小写. 解决这道题的方法很多.可能一百个人有一百个思路.当时第一眼看到这个题我的思路是:先将接受的一串 ...
- Collection was modified; enumeration operation may not execute Dictionary 集合已修改;可能无法执行枚举操作
public void ForeachDic() { Dictionary dic = new Dictionary(); dic.Add("1", 10); dic.Add(&q ...
- Dictionary集合 字典
1 Dictionary<int, string> dic = new Dictionary<int, string>(); dic.Add(,"张三"); ...
- C#泛型集合之Dictionary<k, v>使用技巧
1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value) ...
- ASP.NET MVC中默认Model Binder绑定Action参数为List、Dictionary等集合的实例
在实际的ASP.NET mvc项目开发中,有时会遇到一个参数是一个List.Dictionary等集合类型的情况,默认的情况ASP.NET MVC框架是怎么为我们绑定ASP.NET MVC的Actio ...
- C#泛型集合—Dictionary<K,V>使用技巧
转载:http://blog.csdn.net/a125138/article/details/7742022 1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collec ...
- 转载C#泛型集合—Dictionary<K,V>使用技巧
1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value) ...
- 使用Dictionary泛型集合封装业务逻辑判断 z
C#2.0 提供了Dictionary 泛型类,它提供了从一组键到一组值的映射.字典中的每个添加项都由一个值及其相关联的键组成.通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictio ...
- 04.Dictionary字典键值对集合
Dictionary字典键值对集合和Hashtable键值对集合的功能非常类似, 只是在声明的时候,必须为其制定值的类型. 示例代码: namespace _11.Dictionary字典集合的学习 ...
随机推荐
- 《内蒙古自治区第十三届大学生程序设计竞赛试题_H 公孙玉龙》
这个题有点小坑,最坑的地方就是 输入的b 变量 有可能 是 负数 ! 负数 ! 负数! 对 ,你没有看错,就是负数,坑死我了, 一直都是 content.charAt(0) 判断 ...
- Git 安装和使用Tips
1. git的安装 2. git的常见用法 3. git的常见问题 1. git的安装 1)首先去下载https://git-scm.com/downloads 在上述官网上下载符合自己操作系统的gi ...
- python 在字典中添加键值对的方法。
list 添加元素的方法是 list.append(a).将 a 添加到 list 里. dict 添加元素的方法是 dict.update(dict2).意为,将 dict2 的内容添加到 di ...
- c++中的回调
一:设置一个函数指针,在需要使用的时候调用 #include <iostream> typedef void (__stdcall *DownloadCallback)(const cha ...
- mybatis-generator自动生成Mapper.dao.entity
在pom.xml中添加依赖 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId> ...
- php代码审计——DVWA
1.命令注入: LOW等级: 输入没有任何限制,可以直接ping: Medium等级: 介绍一下str_replace()函数和array_key()函数: Str_replace(str1,str2 ...
- pyqt多线程进度条
ui.py # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'ui.ui' # # Creat ...
- LVM (逻辑卷管理器)
图片来自:https://www.cnblogs.com/linuxprobe/p/5381538.html 参考博客:https://www.cnblogs.com/linuxprobe/p/538 ...
- Android CmakeList
https://www.cnblogs.com/chenxibobo/p/7678389.html
- 使用vue时,报错“exports is not defined”
在开发中引用插件时,报错“exports is not defined” 但在引用第三方组件的时候,在浏览器中报错“exports is not defined”.根据浏览器报错信息,查询到报错来源是 ...