C#编程-89:Hashtable添加键值和遍历_彭世瑜_新浪博客
彭世瑜 发布时间:2017-08-03 22:48:22 ,浏览量:2
- 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();
- }
- }
- }
1665367115
查看更多评论