- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace HashTableTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- Hashtable ht = new Hashtable();
- //添加元素
- ht.Add(1,"jack");
- ht.Add(2,"tom");
- ht[3] = "join";
- //用此种方式添加元素应该注意:
- //如果对应的键key存在,重新赋值
- //如果不存在,则增加对应的键值对
- ht[1] = "mach";
- ht[4]="marry";
- //数组长度:length
- //集合个数:count
- Console.WriteLine(ht.Count);
- //遍历集合的两种方式:
- foreach (DictionaryEntry obj in ht)
- {
- Console.WriteLine("{0} - {1}",obj.Key,obj.Value);
- }
- Console.WriteLine("======================");
- foreach (object obj in ht.Keys)
- {
- Console.WriteLine("{0} - {1}",obj,ht[obj]);
- }
- Console.ReadKey();
- }
- }
- }
C#编程-89:Hashtable添加键值和遍历
最新推荐文章于 2023-07-04 13:26:49 发布