集合-字典(Dictionary)
字典(散列表):允许按照某个键来访问元素,能根据键快速查找元素,也可以自由添加,删除元素。比较像List<T>类,但没有list向后移动元素的性能开销。
.net中最主要的字典类是Dictionary<Tkey,Tvalue>。
字典中的键:用作字典中键的类型必须重写Object类中的GetHashCode()方法。调用GetHashCode()方法主要是为了获得元素的位置。
键还必须实现IEquatable<T>.Equals方法或重写Object类的Equals()方法。因为不同的键代码可能返回相同的散列代码,所以使用equals方法比较键。字典比较两个键是否相等,调用A.Equals(B)方法,如果A.Equals(B)返回true,则A. GetHashCode()和B. GetHashCode()必须返回相同的散列代码!
public class EmployeeIdException : Exception
{
public EmployeeIdException(string message) : base(message) { }
} public class EmployeeId : IEquatable<EmployeeId>
{
private readonly char prefix;
private readonly int number; public EmployeeId(string id)
{
if (id == null || id == "")
{
throw new ArgumentNullException("id");
}
prefix = id.ToUpper()[];
int numberlength = id.Length - ;
try
{
number = int.Parse(id.Substring(, numberlength > ? : numberlength)); }
catch (FormatException)
{
throw new EmployeeIdException("无法格式化ID");
}
} public override string ToString()
{
return prefix.ToString() + string.Format("{0,6:000000}", number);
} public override int GetHashCode()
{
return (number ^ number << ) * 0x15051505;
} public bool Equals(EmployeeId other)
{
return (prefix == other.prefix && number == other.number);
} public override bool Equals(object obj)
{
return Equals((EmployeeId)obj);
} public static bool operator ==(EmployeeId left, EmployeeId right)
{
return left.Equals(right);
} public static bool operator !=(EmployeeId left, EmployeeId right)
{
return !(left == right);
}
} public class Employee
{
private string name;
private double salary;
private EmployeeId id;
public Employee(string name, double salary, EmployeeId id)
{
this.name = name;
this.salary = salary;
this.id = id;
} public override string ToString()
{
return string.Format("{0}:{1,-20}{2:C}", id, name, salary);
}
}
class Program
{
static void Main(string[] args)
{
var employees = new Dictionary<EmployeeId, Employee>(); var idrxm = new EmployeeId("A5");
var rxm = new Employee("rxm", 9.6, idrxm);
employees.Add(idrxm, rxm);
Console.WriteLine(rxm); var idcwr = new EmployeeId("B888888456");
var cwr = new Employee("cwr", 9.5, idcwr);
employees.Add(idcwr, cwr);
Console.WriteLine(cwr); while (true)
{
Console.WriteLine("请输入员工工号,X代表退出!");
var writeTxt = Console.ReadLine();
if (writeTxt.ToLower() == "x")
{
break;
}
EmployeeId id;
try
{
id = new EmployeeId(writeTxt);
Employee employ;
if (!employees.TryGetValue(id, out employ))
{
Console.WriteLine("{0},这个工号的人不存在", id);
}
else
{
Console.WriteLine(employ);
}
}
catch (EmployeeIdException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
集合-字典(Dictionary)的更多相关文章
- C#中数组、集合(ArrayList)、泛型集合List<T>、字典(dictionary<TKey,TValue>)全面对比
C#中数组.集合(ArrayList).泛型集合List<T>.字典(dictionary<TKey,TValue>)全面对比 为什么把这4个东西放在一起来说,因为c#中的这4 ...
- python 数据类型: 字符串String / 列表List / 元组Tuple / 集合Set / 字典Dictionary
#python中标准数据类型 字符串String 列表List 元组Tuple 集合Set 字典Dictionary 铭记:变量无类型,对象有类型 #单个变量赋值 countn00 = '; #整数 ...
- 索引器、哈希表Hashtabl、字典Dictionary(转)
一.索引器 索引器类似于属性,不同之处在于它们的get访问器采用参数.要声明类或结构上的索引器,使用this关键字. 示例: 索引器示例代码 /// <summary> /// 存储星 ...
- C# 字典 Dictionary<Tkey,Tvalue>
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...
- C# .Net 中字典Dictionary<TKey,TValue>泛型类 学习浅谈
一.综述: Dictionary<TKey,TValue>是在 .NET Framework 2.0 版中是新增的.表示键值对的集合,Dictionary<TKey,TValue&g ...
- 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密
你真的了解字典(Dictionary)吗? 从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...
- [Python]字典Dictionary、列表List、元组Tuple差异化理解
概述:Python中这三种形式的定义相近,易于混淆,应注意区分. aDict={'a':1, 'b':2, 'c':3, 'd':4, 'e':5} aList=[1,2,3,4,5] aTuple= ...
- C#字典 Dictionary<Tkey,Tvalue> 之线程安全问题 ConcurrentDictionary<Tkey,Tvalue> 多线程字典
ConcurrentDictionary<Tkey,Tvalue> Model #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutra ...
- C#对字典Dictionary 的添加,遍历,移除系列操作
C#对字典Dictionary 的添加,遍历,移除系列操作: //一.创建泛型哈希表,然后加入元素 Dictionary<string, string> oscar = new Dicti ...
- C#创建安全的字典(Dictionary)存储结构
在上面介绍过栈(Stack)的存储结构,接下来介绍另一种存储结构字典(Dictionary). 字典(Dictionary)里面的每一个元素都是一个键值对(由二个元素组成:键和值) 键必须是唯一的,而 ...
随机推荐
- jQuery第二章
一.jQuery选择器 jQuery的行为规则都必须在获取到元素后才能生效.来看一个简单的例子: <p class = “demo”>jQuery Demo</p> <s ...
- hdu_3063_Play game(数论)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3063 题意:中文题,说的很清楚,不解释 题解:公式题,具体看代码 #include<stdio. ...
- Infix to postfix conversion 中缀表达式转换为后缀表达式
Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...
- callback in C
callback is nothing but passing the function pointer to the code from where you want your handler/ c ...
- mongo细节
mongo创建表db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean>, size: < ...
- 用 openSSL 生成 公钥 私钥
支付宝app接口需要 RSA加密通讯 https://doc.open.alipay.com/doc2/detail?treeId=58&articleId=103242&docTyp ...
- 抛弃jQuery,拥抱原生JavaScript
前端发展很快,现代浏览器原生 API 已经足够好用.我们并不需要为了操作 DOM.Event 等再学习一下 jQuery 的 API.同时由于 React.Angular.Vue 等框架的流行,直接操 ...
- MVC动态生成表单
1*书写方式 一.using语句可以不写结束标记,自动加上 服务端 客户端 默认提交当前控制器和操作方法 二.开始与结束代码都写 服务端 客户端 三.一些常用的重载方法 (1)要提交的控制器,和操作方 ...
- byte数组如何转为short数组 (转)
最近在搞毕业设计,做的是有关语音识别的手机应用.在处理音频的过程中,发现需要用short数组处理音频,可能光用byte会越界.但是java读文件没有一次性读到short数组中的api,要么是一个一个读 ...
- 判断是ios还是android
//判断是ios还是androidvar system;var ua = navigator.userAgent.toLowerCase(); if (/iphone|ipad|ipod/.test( ...