C#集合之Hashtable
Hashtable是一个键值对集合,其泛型版本是Dictionary<K, V>,下面说下常用的一些方法;
1.Add(),向Hashtable添加元素,需要注意的是因为它是键值对集合,所以要同时添加一个键和一个值,且键不能重复。
2.ContainsKey(key)方法,是查询是否包含某个键,其Contains()方法最终也是调用这个方法实现的。
3.ContainsValue(value)方法,是查询是否包含某个值。
4.hash[“key”],根据键获取某个值。hash[“key”]=“修改”,根据键修改对应的值。
5.Remove(“key”);根据键删除对应的值。
需要注意的是,以上3个方法,其泛型版本Dictionary<K, V>也是一样的。
Keys属性,是Hashtable的键集合。Values属性,是Hashtable的值集合;可以通过foreach遍历它的键集合和值集合。
如果要同时得到Hashtable中的键集合和值集合,在使用foreach遍历时,请将var关键字替换为DictionaryEntry,这样就能同时拿到键和值了。
而对于Dictionary<K, V>要同时得到键集合和值集合,在使用foreach遍历时,请将var关键字替换为KeyValuePair<k,v>。
键值对集合,如果得到键,可以使用 集合名[键名]这样的索引方式获取对应的值。因为键值对集合不提供下标来访问,故这类集合不能通过for循环来遍历。
下面看几个例子程序,以便更好理解其实际应用:
1.将int数组中的奇数放到一个新的int数组中返回
int[] iArray = new int[] { 2, 7, 8, 3, 22, 9, 5, 11, 14, 18, 21 };
ArrayList aLst = new ArrayList();
for (int i = 0; i < iArray.Length; i++)
{
if (iArray[i] % 2 != 0)
{
aLst.Add(iArray[i]);
}
}
object[] obj = aLst.ToArray();
foreach (var item in obj)
{
Console.WriteLine(item);
}
Console.ReadKey();
2.从一个整数的List<int>中取出最大数(找最大值)(最简单是调用List集合的max方法)
private static int GetMax(List<int> iList)
{
int iMax = iList[];
for (int i = ; i < iList.Count; i++)
{
if (iList[i]>iMax)
{
iMax = iList[i];
}
}
return iMax;
}
3.把123转换为:壹贰叁。
/// <summary>
/// 数字转换成大写汉字
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
private static string ConvertInt(string msg)
{
string s = "0零 1壹 2贰 3叁 4肆 5伍 6陆 7柒 8扒 9玖";
Dictionary<char, char> dic = new Dictionary<char, char>(); string[] strArray = s.Split(' ');
for (int i = ; i < strArray.Length; i++)
{
dic.Add(strArray[i][], strArray[i][]);
} StringBuilder sb = new StringBuilder(); for (int i = ; i < msg.Length; i++)
{
if (dic.ContainsKey(msg[i]))
{
sb.Append(dic[msg[i]]);
}
else
{
sb.Append(msg[i]);
}
}
return sb.ToString();
}
4.计算字符串中每种字母出现的次数(面试题)。 “Welcome ,to Chinaworld”
Dictionary<char, int> dic = new Dictionary<char, int>();
for (int i = ; i < msg.Length; i++)
{
if (!dic.ContainsKey(msg[i]))
{
dic.Add(msg[i], );
}
else
{
dic[msg[i]] = dic[msg[i]] + ;
}
}
foreach (KeyValuePair<char, int> item in dic)
{
Console.WriteLine("字符<{0}>在文本中出现了{1}次", item.Key, item.Value);
}
延伸,如果是要统计一段中文中,某个词组呢?
string msg = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”";
string key = "咳嗽";
int iIndex = msg.IndexOf(key, );
if (iIndex!=-)
{
Console.WriteLine("字符{0}第1次出现的位置是:{1}", key, iIndex);
} int iCount = ;
while (iIndex != -)
{
iIndex = msg.IndexOf(key, iIndex + key.Length);
if (iIndex != -)
{
iCount++;
Console.WriteLine("字符{0}第{1}次出现的位置是:{2}", key, iCount, iIndex);
}
else
{
break;
}
}
5.编写一个函数进行日期转换,将输入的中文日期转换为阿拉伯数字日期,比如:二零一二年十二月月二十一日要转换为2012-12-21
private static string ConvertDate(string strDate)
{
Dictionary<char, char> dic = new Dictionary<char, char>();
string s = "零0 一1 二2 三3 四4 五5 六6 七7 八8 九9";
string[] strArray = s.Split(' ');
for (int i = ; i < strArray.Length; i++)
{
dic.Add(strArray[i][], strArray[i][]);
} StringBuilder sb = new StringBuilder();
for (int j = ; j < strDate.Length; j++)
{
if (dic.ContainsKey(strDate[j]))
{
sb.Append(dic[strDate[j]]);
}
else
{
if (strDate[j]=='十')
{
//1.十 10 10
//2.二十 20 0
//3.十二 12 1
//4.二十二 22 不翻译 if (!dic.ContainsKey(strDate[j - ]) && !dic.ContainsKey(strDate[j + ]))
{
sb.Append("");
}
else if (dic.ContainsKey(strDate[j - ]) && !dic.ContainsKey(strDate[j + ]))
{
sb.Append("");
}
else if (!dic.ContainsKey(strDate[j - ]) && dic.ContainsKey(strDate[j + ]))
{
sb.Append("");
}
else if (dic.ContainsKey(strDate[j - ]) && dic.ContainsKey(strDate[j + ]))
{ }
}
else
{
sb.Append(strDate[j]);
}
}
}
string n = sb.ToString().Replace('年','-');
n = n.Replace('月', '-');
return n.Replace('日',' ');
}
上面代码中的中文注释还对不同情况做了判断。
关于集合,先就说这么多,写出来算是给自己的总结,留作他日复习用。如果各路大神有补充,请跟我联系,共同进步啊!
C#集合之Hashtable的更多相关文章
- C#基础课程之五集合(HashTable,Dictionary)
HashTable例子: #region HashTable #region Add Hashtable hashTable = new Hashtable(); Hashtable hashTabl ...
- 从内部剖析C#集合之HashTable
计划写几篇文章专门介绍HashTable,Dictionary,HashSet,SortedList,List 等集合对象,从内部剖析原理,以便在实际应用中有针对性的选择使用. 这篇文章先介绍Hash ...
- 从内部剖析C# 集合之---- HashTable
这是我在博客园的第一篇文章,写的不好或有错误的地方,望各位大牛指出,不甚感激. 计划写几篇文章专门介绍HashTable,Dictionary,HashSet,SortedList,List 等集合对 ...
- Stack集合、queue集合、hashtable集合
1.栈:Stack,先进后出,一个一个赋值,一个一个取值,按顺序. .count 取集合内元素的个数 .push() 将元素一个一个推入集合中//stack集合存入 ...
- Java集合之Hashtable
和HashMap一样,Hashtable也是一个散列表,存储的内容也是键值对key-value映射.它继承了Dictionary,并实现了Map.Cloneable.io.Serializable接口 ...
- Map集合、HashMap集合、LinkedHashMap集合、Hashtable集合、Collections工具类和模拟斗地主洗牌和发牌
1.Map集合概述和特点 * A:Map接口概述 * 查看API可以知道: * 将键映射到值的对象 * 一个映射不能包含重复的键 * 每个键最多 ...
- Java集合——HashMap,HashTable,ConcurrentHashMap区别
Map:“键值”对映射的抽象接口.该映射不包括重复的键,一个键对应一个值. SortedMap:有序的键值对接口,继承Map接口. NavigableMap:继承SortedMap,具有了针对给定搜索 ...
- C#中的集合(HashTable与Array类)【转】
一.Array类 1.Array类的属性 序号 属性 & 描述 1 IsFixedSize 获取一个值,该值指示数组是否带有固定大小. 2 IsReadOnly 获取一个值,该值指示数组是否只 ...
- 集合之HashTable
在java中与有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key)方法获取相 ...
随机推荐
- Hadoop概念学习系列之URI深入(三十二)
ls / ------------------------ 这是查本地Linux上的根 hadoop fs -ls / ------------- 这是查hdfs上的根 或者, had ...
- SQL2008-删除时间字段重复的方法
ID EMAgitation_ID YieldDateTime 1 2 2012-02-11 10:18:54.0002 2 ...
- MongoDB介绍及下载与安装
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型.M ...
- ios键盘上添加辅助视图
- Linux 下 scp 传输文件脚本
脚本执行效果: (1).远程传输本地 /ora_exp/dmp/CWDB_RAMS_* 文件至 11.4.24.21 的 /ora_exp/dmp 目录下. 脚本编写步骤: 假设 oracle 用 ...
- ECSHOP在线手册布局参考图--积分商城 exchange_list.dwt
A.购物车 1,设置方法 程序自动读取购物车的商品数量 2,代码相关 cart.lbi 中 {insert_scripts files='transport.js'} <div clas ...
- JAVA js的escape函数、解析用js encodeURI编码的字符串、utf8转gb2312的函数
在使用webView时,如果url中参数有中文的话,拦截到的字符串就会类似这样的:http://api.letstar.cn/zq/news.html?id=20&cupName=%E6%B5 ...
- iOS开发——新特性OC篇&Swift 2.0新特性
Swift 2.0新特性 转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心.我同样也是非常喜爱这门新的编程语言. 今年6月,一年一度 ...
- MySQL如何选择float, double, decimal
http://yongxiong.leanote.com/post/mysql_float_double_decimal
- 简洁判断一个byte中有多少位为1的bit?
以下是Brian W. Kernighan公开的一个方法 unsigned bit_count(unsigned v) { unsigned int c; //置位总数累计 ; v; c++) { v ...