键值对是用Hashtable,还是Liat<>。求键值对使用实例。
功能描述:
1、添加键值对
2、删除指定键值对
3、修改指定键值对
4、根据键来读取值
功能描述:
1、添加键值对
2、删除指定键值对
3、修改指定键值对
4、根据键来读取值
解决方案 »
- 求一正则表达式,高手来
- 找不到图片的来源
- 使用dataGridView遇到的问题(急)
- 哪位仁兄能帮我解决一点小问题啊?
- 怎么在一个代码段内增加一个延迟时间?
- 三个小问题,请教一下
- C# post servlet中 Stream stream = HttpWebRequest.GetRequestStream()出错
- 请教:CRL开发无法引用System.Windows.Forms.RichTextBox吗?
- 请教
- 讨论Csharp写的Sqlserver论坛承受度
- C# WinForm 一个Login窗体,登陆后显示主窗体,用户可以以不同的权限登陆,登陆后在主窗体的最下面显示登录者的权限,身份
- 如何将xml文件映射成一个实体类?
/// SortedDictionary 的摘要说明。
/// </summary>
public class SortedDictionary : DictionaryBase
{
ArrayList list; public SortedDictionary() {
list = new ArrayList();
}
public String this[ String key ] {
get {
return( (String) Dictionary[key] );
}
set {
if( !this.list.Contains( key ) )
this.list.Add( key );
Dictionary[key] = value;
}
} public ICollection Keys {
get {
return( this.list );
}
} public ICollection Values {
get {
return( Dictionary.Values );
}
} public void Add( String key, String value ) {
Dictionary.Add( key, value );
} public bool Contains( String key ) {
return( Dictionary.Contains( key ) );
} public void Remove( String key ) {
Dictionary.Remove( key );
} protected override void OnInsert(object key, object value) {
this.list.Add( key );
} protected override void OnRemove(object key, object value) {
this.list.Remove( key );
mylist.Add("key1","benjing"); Hashtable mytable=new Hashtable()
mytable.Add("key1","shandong");4.其中SortedList为可排序的字典,当添加元素时,元素按照正确的排序顺序插入SortedList,同时索引自动进行相应的调整,移除元素亦然。using System;
using System.Collections;
class Class1
{
static void Main()
{
Hashtable mHash = new Hashtable();
SortedList mSort = new SortedList();
for (int i = 0; i < 15; i++)
{
string oKey = "Key " + i.ToString("D4");
string oValue = "Value " + i.ToString("x4");
mHash.Add(oKey, oValue);
mSort.Add("Key " + i.ToString("D2"), "Val " + i.ToString("X2"));
} WrHlist(mHash);
WriteList(mSort);
Console.ReadLine();
}
public static void WrHlist(Hashtable h)
{
IDictionaryEnumerator mIDE = h.GetEnumerator();
while (mIDE.MoveNext())
{
Console.WriteLine("{0}:{1}", mIDE.Key, mIDE.Value);
}
}
public static void WriteList(SortedList mSor)
{
Console.WriteLine(" --以下为集合中所包含的对象元素键对!!-- ");
for (int i = 0; i < 10; i++)
{
Console.WriteLine("mSotr[{0}]的值为:{1}", mSor.GetKey(i), mSor["Key " +i]);
}
}
}5.字典的遍历:使用DictionaryEntry对象eg:foreach(DictionaryEntry dic in mylist) //这里的mylist为哈希表初始化并增加键值后的集合。
{
label1.text=label2.text+dic.Value.ToString()+dic.Key.ToString();
}
或 Dictionary<object,object> lst 求键值对使用实例。
功能描述:
1、添加键值对 lst[key] = value;
2、删除指定键值对 lst.Remove()
3、修改指定键值对 lst[key] = value;
4、根据键来读取值 lst[Key]如果类型确定,用 dictionary<>最好。
还有个SortList ,是数组和dictionary的组合,也可以用。