C# Dictionary使用
2024-08-22 14:47:09
Dictionary<string, int> illegParking = new Dictionary<string, int>();
键:inData.LOTID
值:inData.ISILLEGPARKING
1、判断键存不存在。
dictionary中是不允许有重复项的,这样才能按key索引到唯一一个value。
if (illegParking.ContainsKey(inData.LOTID))
{
illegParking[inData.LOTID] = inData.ISILLEGPARKING;
}
else
{
illegParking.Add(inData.LOTID, inData.ISILLEGPARKING);
}
2、几种遍历方式:
Dictionary<string, int> list = new Dictionary<string, int>(); foreach (var item in list) { Console.WriteLine(item.Key + item.Value); } //通过键的集合取 foreach (string key in list.Keys) { Console.WriteLine(key + list[key]); } //直接取值 foreach (int val in list.Values) { Console.WriteLine(val); } //非要采用for的方法也可 Dictionary<string, int> list = new Dictionary<string, int>(); List<string> test = new List<string>(list.Keys); for (int i = ; i < list.Count; i++) { Console.WriteLine(test[i] + list[test[i]]); }
3、涉及到移除某个键值的时候
不能在foreach循环里面移除,因为会导致错误:集合已修改;可能无法执行枚举操作。可以改用for循环
//dicmodels是个dictionary
List<string> keys = new List<string>(dicModels.Keys);
for (int i = keys.Count - ; i >= ; i--)
{
}
最新文章
- 计算机程序的思维逻辑 (33) - Joda-Time
- 最新的JavaScript核心语言标准&mdash;&mdash;ES6,彻底改变你编写JS代码的方式!【转载+整理】
- Hadoop入门进阶课程3--Hadoop2.X64位环境搭建
- QStyle 新风格的实现
- ubuntu无法进入和引导顺序问题解决
- 盒模型Box Model(浮动)
- 从Java小白到阿里巴巴工程师,回顾我两年来的学习经历
- Servlet(三):生命周期、常用方法、常见错误
- 前端使用 Nginx 反向代理彻底解决跨域问题
- python:实例化configparser模块读写配置文件
- [shell]find命令
- [C#.NET]最简单的实现文本框的水印效果
- io类型
- 全网最详细的PLSQL Developer + Oracle client的客户端 或者 PLSQL Developer + Oracle server服务端的下载与安装过程(图文详解)
- Java系统和PHP系统相互调用
- Android 投射工具和录屏工具
- 实时更新数据,无需刷新:a,如何使用Turbolinks clearCache(), b Action Cable
- 【12c OCP】CUUG OCP认证071考试原题解析(36)
- MAC下常用命令的中文帮助文档(man) 出现错误
- python之md5模块
热门文章
- [LOJ#3120][Luogu5401][CTS2019]珍珠(容斥+生成函数)
- 单实例dg软件从10.2.0.4版本安装至10.2.0.5.12
- 记redis一次Could not get a resource from the pool 异常的解决过程
- 阿里企业邮箱smtp设置
- 【转载】 C#中List集合使用InsertRange方法在指定的位置插入另一个list集合
- node的启动环境
- 聊聊GIS中的坐标系|再版
- Fortify漏洞之Denial of Service: Regular Expression
- UCOSIII互斥信号量
- Js网站开发学习第一天