哈希表类的键和数值的检索(C#) // 吴新强 实验小结 由于哈希表类的键和数值的检索,于2013年3月16日21:02:37
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace Chapter10_HashTable
{
class Program
{
static void Main(string[] args)
{
Hashtable symbols = new Hashtable(25);
symbols.Add("salary", 1000);
symbols.Add("name", "John");
symbols.Add("age", 43);
symbols.Add("dept", "Computer");
symbols["sex"] = "Man";
Console.WriteLine("The keys are :");
foreach (Object key in symbols.Keys)
Console.WriteLine(key);
Console.WriteLine();
Console.WriteLine("Zhe values are:");
foreach (Object value in symbols.Values)
Console.WriteLine(value);
Console.WriteLine();
// symbols.Clear();
// symbols.Remove("name");
string aKey;
Console.WriteLine("Enter a key to remove :");// 利用键值删除键值对
aKey = Console.ReadLine();
if (symbols.ContainsKey(aKey))
symbols.Remove(aKey);
string aValue;
Console.WriteLine("Enter a Value to remove :"); // 利用数值删除键值对
aValue = Console.ReadLine();
if (symbols.ContainsKey(aValue))
symbols.Remove(aValue);
int count= symbols.Count;
Console.WriteLine("哈希表的内容如下:");
foreach (object key in symbols.Keys)
Console.WriteLine(key.ToString() + ":" + symbols[key].ToString());
Console.WriteLine();