HashTable、List、ArrayList的经典使用和相互转换
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("---------遍历哈希表中的值----------");
|
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的经典使用和相互转换的更多相关文章
- hashmap和hashtable,arraylist和vector的区别
hashmap线程不安全,hashtable线程安全 hashmap允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同. ...
- HashMap、HashTable、ArrayList、LinkedList、Vector区别
HashTable和HashMap区别 ①继承不同. public class Hashtable extends Dictionary implements Map public class Has ...
- Vector & ArrayList Hashtable & HashMap ArrayList & LinkedList
1. Vector & ArrayList 1) Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的方法不是,由于线程的 ...
- List、dictionary、hashtable、ArrayList集合
集合的引用命名空间在 system.Collections下 1.为什么引入集合 因为数组长度是固定的,为了建立一个动态的"数组",所以引入了集合. 2.为什么引入ArrayLis ...
- ArrayList、HashTable、List、Dictionary的演化及如何选择使用
在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...
- 利用ArrayList对Hashtable其进行排序
前言: 最近在使用Hashtable的时候发现一个问题:就是当你对Hashtable进行遍历的时候整个输出结果是毫无顺序的, 上网查了一下说是Hashtable有自己内部的排序机制,如果要自定义排序的 ...
- ArrayList、HashSet、HashTable、List、Dictionary的区别
在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...
- 数组、ArrayList、HashTable
相同点:都可以存储一组数据 不同点: 1)数组,必须要先分配空间,存储数据固定 2)ArrayList,存储数据可以自由扩展 3)HashTable与ArrayList一样,但是它是有意义的,键值对形 ...
- C# ArrayList、HashSet、HashTable、List、Dictionary的区别
在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...
随机推荐
- WebService—CXF整合Spring实现接口发布和调用过程
一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...
- Objective-C:NSDectionary字典的常见操作
NSDectionary字典:它是一个存储键值的容器,每一个键key都对应着一个值value,可以通过键key一次性找到目标值value,这是一个比较好的存储器,相比于数组而言,它明显提高了查询效率. ...
- [18] 螺旋楼梯(Spiral Stairs)图形的生成算法
顶点数据的生成 bool YfBuildSpiralStairsVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint sli ...
- An easier way to debug windows services
Have you got tired of attaching the Visual Studio debugger to the service application? I got the sol ...
- HDU1226:超级密码(BFS)
Problem Description Ignatius花了一个星期的时间终于找到了传说中的宝藏,宝藏被放在一个房间里,房间的门用密码锁起来了,在门旁边的墙上有一些关于密码的提示信息: 密码是一个C进 ...
- go语言之进阶篇同名字段
1.同名字段 示例: package main import "fmt" type Person struct { name string //名字 sex byte //性别, ...
- iOS开发-舒尔特表
周末闲来无事,看一个概念,挺有意思的,舒尔特表,网上也有很多人写过类似的Demo,本人闲来无事也写了一下,舒尔特表听起来很高大上的样子,不过本人的理解就是一个正方形的矩阵中放的各种小格子,可以是字母, ...
- Prototype 原型模式 复制 浅拷贝 clone MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 图解vue中 v-for 的 :key 的作用,虚拟dom Diff算法
其实不只是vue,react中在执行列表渲染时也会要求给每个组件添加上key这个属性. 要解释key的作用,不得不先介绍一下虚拟DOM的Diff算法了. 我们知道,vue和react都实现了一套虚拟D ...
- Android -- 跳转应用市场评分
Code Uri uri = Uri.parse("market://details?id="+getPackageName()); Intent intent = new Int ...