1、添加引用 using System.Collections;

2、创建并添加数据

             Hashtable hs = new Hashtable();
hs.Add("Name1", "lwj");
hs.Add("Name2", "wyp");
hs.Add("Name3", "zwl");
hs.Add("Name4", "zyc");
hs.Add("Name8", "wyw");
hs.Add("Name5", "wyw");

3、遍历哈希表中的 值Value

 Console.WriteLine("---------遍历哈希表中的值----------");
////返回循环访问 System.Collections.Hashtable 的 System.Collections.IDictionaryEnumerator。
IDictionaryEnumerator emu = hs.GetEnumerator(); //// 遍历哈希表所有的键,读出相应的值
while (emu.MoveNext()) //如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
{
string str = emu.Value.ToString();
Console.WriteLine(str);
}

4、遍历哈希表

 Console.WriteLine("-----------遍历哈希表-------------");
foreach (DictionaryEntry de in hs)
{
Console.WriteLine("key = {0}; Value = {1}", de.Key, de.Value);
}

5、对HashTable排序之后输出,按Key排序

 Console.WriteLine("-------哈希表按Key排序之后--------");
////哈希表排序, 按Key排序
ArrayList alist = new ArrayList(hs.Keys);
alist.Sort();
foreach (string obj in alist) // //遍历alist
{
Console.WriteLine("{0, -15} {1, -15}", obj, hs[obj]); //{0, -15} PadLeft
}

6、转换成List输出

 Console.WriteLine("------------使用List之后-----------");
List<string> list = new List<string>();
foreach (DictionaryEntry de in hs)
{
list.Add(de.Value.ToString());
}
foreach (string obj in list)
{
Console.WriteLine(obj);//i就是下标
}

7、转换成Dictionary<object, object>之后进行遍历输出

 Console.WriteLine("---------使用Dictionary之后--------");
Dictionary<object, object> myDic = new Dictionary<object, object>();
foreach (DictionaryEntry de in hs) //循环遍历HashTable 将其添加至 myDic
{
myDic.Add(de.Key, de.Value); // 这俩都是 object型的
}
//循环遍历输出 myDic
foreach (object obj in myDic)
{
Console.WriteLine(obj.ToString()); //[Name,wyw] 输出是这样的格式
}
//采用另一种输出
Console.WriteLine("--------Dictionary键值对输---------");
foreach (KeyValuePair<object, object> kvp in myDic)
{
Console.WriteLine("Key={0}, Value={1}", kvp.Key, kvp.Value); //获取键值对后,自定义输出
}

8、克隆HashTable 到另一HashTable并 遍历输出:

 //此处写 添加 删除一些数据,以便验证
1 Console.WriteLine("------添加移除之后,遍历哈希表-----");
hs["Name0"] = "The First";
hs.Remove("Name8");
hs.Add("Name6", "add6");
foreach (DictionaryEntry de in hs)
{
Console.WriteLine("Key: {0, 15} Value{1, 15}", de.Key, de.Value);
} Console.WriteLine("--------克隆哈希表到hs2之后--------");
Hashtable hs2 = (Hashtable)hs.Clone(); //public virtual object Clone(); 创建 System.Collections.Hashtable 的浅表副本。
foreach (DictionaryEntry de in hs2)
{
Console.WriteLine("Key: {0, 15} Value{1, 15}", de.Key, de.Value);
} Console.WriteLine("----------哈希表清空之后-----------");
hs.Clear();
foreach (DictionaryEntry de in hs) //哈希表存在,但里面没数据,因此下列不执行
{
Console.WriteLine("Key: {0, 15} Value{1, 15}", de.Key, de.Value);
}

输出结果:

全部源代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections; namespace HashTable
{
class Program
{
static void Main(string[] args)
{
Hashtable hs = new Hashtable();
hs.Add("Name1", "lwj");
hs.Add("Name2", "wyp");
hs.Add("Name3", "zwl");
hs.Add("Name4", "zyc");
hs.Add("Name8", "wyw");
hs.Add("Name5", "wyw"); Console.WriteLine("-----------遍历哈希表-------------");
foreach (DictionaryEntry de in hs)
{
Console.WriteLine("key = {0}; Value = {1}", de.Key, de.Value);
} Console.WriteLine("---------遍历哈希表中的值----------");
////返回循环访问 System.Collections.Hashtable 的 System.Collections.IDictionaryEnumerator。
IDictionaryEnumerator emu = hs.GetEnumerator(); //// 遍历哈希表所有的键,读出相应的值
while (emu.MoveNext()) //如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
{
string str = emu.Value.ToString();
Console.WriteLine(str);
} Console.WriteLine("-------哈希表按Key排序之后--------");
////哈希表排序, 按Key排序
ArrayList alist = new ArrayList(hs.Keys);
alist.Sort();
foreach (string obj in alist) // //遍历alist
{
Console.WriteLine("{0, -15} {1, -15}", obj, hs[obj]); //{0, -15} PadLeft
} Console.WriteLine("------------使用List之后-----------");
List<string> list = new List<string>();
foreach (DictionaryEntry de in hs)
{
list.Add(de.Value.ToString());
}
foreach (string obj in list)
{
Console.WriteLine(obj);//i就是下标
} Console.WriteLine("---------使用Dictionary之后--------");
Dictionary<object, object> myDic = new Dictionary<object, object>();
foreach (DictionaryEntry de in hs) //循环遍历HashTable 将其添加至 myDic
{
myDic.Add(de.Key, de.Value); // 这俩都是 object型的
}
//循环遍历输出 myDic
foreach (object obj in myDic)
{
Console.WriteLine(obj.ToString()); //[Name,wyw] 输出是这样的格式
}
//采用另一种输出
Console.WriteLine("--------Dictionary键值对输---------");
foreach (KeyValuePair<object, object> kvp in myDic)
{
Console.WriteLine("Key={0}, Value={1}", kvp.Key, kvp.Value); //获取键值对后,自定义输出
} Console.WriteLine("------添加移除之后,遍历哈希表-----");
hs["Name0"] = "The First";
hs.Remove("Name8");
hs.Add("Name6", "add6");
foreach (DictionaryEntry de in hs)
{
Console.WriteLine("Key: {0, 15} Value{1, 15}", de.Key, de.Value);
} Console.WriteLine("--------克隆哈希表到hs2之后--------");
Hashtable hs2 = (Hashtable)hs.Clone(); //public virtual object Clone(); 创建 System.Collections.Hashtable 的浅表副本。
foreach (DictionaryEntry de in hs2)
{
Console.WriteLine("Key: {0, 15} Value{1, 15}", de.Key, de.Value);
} Console.WriteLine("----------哈希表清空之后-----------");
hs.Clear();
foreach (DictionaryEntry de in hs) //哈希表存在,但里面没数据,因此下列不执行
{
Console.WriteLine("Key: {0, 15} Value{1, 15}", de.Key, de.Value);
} Console.ReadKey();
}
}
}

笔者在接触到了HashTable之后,上网搜索了一下具体用法,然后笔者就试着去联系以前 接触过的List、Diationary等,

试着将他们相互转换。以上仅供参考,方案并非最佳,希望能帮助大家!谢谢阅读与指正。

HashTable、List、ArrayList的经典使用和相互转换的更多相关文章

  1. hashmap和hashtable,arraylist和vector的区别

    hashmap线程不安全,hashtable线程安全 hashmap允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同. ...

  2. HashMap、HashTable、ArrayList、LinkedList、Vector区别

    HashTable和HashMap区别 ①继承不同. public class Hashtable extends Dictionary implements Map public class Has ...

  3. Vector & ArrayList Hashtable & HashMap ArrayList & LinkedList

    1. Vector & ArrayList 1)  Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的方法不是,由于线程的 ...

  4. List、dictionary、hashtable、ArrayList集合

    集合的引用命名空间在 system.Collections下 1.为什么引入集合 因为数组长度是固定的,为了建立一个动态的"数组",所以引入了集合. 2.为什么引入ArrayLis ...

  5. ArrayList、HashTable、List、Dictionary的演化及如何选择使用

    在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...

  6. 利用ArrayList对Hashtable其进行排序

    前言: 最近在使用Hashtable的时候发现一个问题:就是当你对Hashtable进行遍历的时候整个输出结果是毫无顺序的, 上网查了一下说是Hashtable有自己内部的排序机制,如果要自定义排序的 ...

  7. ArrayList、HashSet、HashTable、List、Dictionary的区别

    在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...

  8. 数组、ArrayList、HashTable

    相同点:都可以存储一组数据 不同点: 1)数组,必须要先分配空间,存储数据固定 2)ArrayList,存储数据可以自由扩展 3)HashTable与ArrayList一样,但是它是有意义的,键值对形 ...

  9. C# ArrayList、HashSet、HashTable、List、Dictionary的区别

    在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...

随机推荐

  1. Windows Server 2003 IIS设置完全篇

    一.启用Asp支持Windows Server 2003 默认安装,是不安装 IIS 6 的,需要另外安装.安装完 IIS 6,还需要单独开启对于 ASP 的支持. 第一步,启用Asp,进入:控制面板 ...

  2. dao层的泛型实现(2种方法)

    一: package com.wzs.test2.dao; import java.util.List; public interface CommonDAO { public <T> v ...

  3. RHPAM 7.2安装

    1.产品架构 2.安装过程 下载相应介质 JBoss EAP (jboss-eap-7.2.0.zip)   下载地址 https://developers.redhat.com/products/e ...

  4. Sequential projection learning for hashing阅读笔记

    真不能再挖坑了,前面挖聊很多坑都没来得及填,从今往后,能写多少就是多少.Sequential projection learning for hashing这篇文章去年就阅读了,当时阅读完没来得及做笔 ...

  5. Linux中Shell的执行流程

    Shell执行流程 1.Printthe info of reminding 打印提示信息 2.Waitinguser for input(wait) 等待用户输入 3.Acceptthe comma ...

  6. 3D屏保程序:汉诺塔

    学过程序的人一定记得汉诺塔.我们学的第一个程序是HelloWorld,而碰到的第一个坑就是汉诺塔,短短十几行代码,不知花费了多少时间精力去理解.我记得当年的开发环境还是蓝屏的,汉诺塔程序的输出还是一行 ...

  7. [3] 球(Sphere)图形的生成算法

    顶点数据的生成 bool YfBuildSphereVertices ( Yreal radius, Yuint slices, Yuint stacks, YeOriginPose originPo ...

  8. JavaScripts基础

    JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.( ...

  9. Android导航抽屉-Navigation Drawer

    Google今年七月份的时候更新了他们的Google+应用,采用了新的导航方式并抛弃了navigationdrawer.一时之间,又引发了一系列关于NavigationDrawer利弊的讨论,不过对于 ...

  10. API手册 常用功能

    directive [ng] a form input input [checkbox] input [email] input [number] input [radio] input [text] ...