C# HashTable集合遍历
作者:秋名
撰写时间:2020 年05 月27日
技术点:Hashtable类表示键/值对的集合,这些键/值对根据键的哈希代码进行组织, 每个元素都是一个存储在 DictionaryEntry 对象中的键/值对。键不能为 null,但值可以。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashTable集合
{
class Program
{
static void Main(string[] args)
{
#region 遍历集合
Hashtable hash = new Hashtable();
hash.Add("h1", "黄三");
hash.Add("xzl", new person() { Name = "许正龙" });
if (!hash.ContainsKey("h1"))//判断是否重复
{//键值对集合的"键"一定不可重复(保证唯一)
hash.Add("h1", "2222");
}
//通过键获取值
Console.WriteLine(hash["h1"].ToString());
person p = hash["xzl"] as person;
Console.WriteLine(p.Name);
Console.WriteLine("====================================================");
//遍历键
foreach (object item in hash.Keys)
{
Console.WriteLine("键:{0}——》值:{1}", item, hash[item]);
}
Console.WriteLine("====================================================");
//遍历值
foreach (object item in hash.Values)
{
Console.WriteLine("值:{0},item");
}
Console.WriteLine("====================================================");
//遍历键值对
foreach (DictionaryEntry kv in hash)
{
Console.WriteLine("键:{0} 值:{1}", kv.Key, kv.Value);
}
#endregion
#region 不使用遍历,提取集合元素
Console.WriteLine("====================================================");
int[] arr = { 15, 175, 264, 350, 401, 598 };
int number = 359;
int index = number / 100;
if (index>=0&&index<arr.Length)
{
Console.WriteLine("存在这个值,索引位置是:{0},对应的值是:{1}",index,arr[index]);
}
#endregion
#region 案例1
Console.WriteLine("====================================================");
//案例1:两个(ArrayList)集合{"a","b","c","d","e"}和{"d","e","f","g","h"},把这两个集合去除重复项合并成一个
ArrayList A1 = new ArrayList(new string[] { "a", "b", "c", "d", "e" });//集合初始化器
ArrayList A2 = new ArrayList(new string[] { "d", "e", "f", "g", "h" });//集合初始化器
for (int i = 0; i < A2.Count; i++)
{
if (!A1.Contains(A2[i]))//Contains判段元素是否存在。
{
A1.Add(A2[i]);//添加
}
}
Console.WriteLine("集合元素个数{0}",A1.Count);
for (int i = 0; i < A1.Count; i++)
{
Console.WriteLine(A1[i]+",");
}
#endregion
#region 案例2
Console.WriteLine("====================================================");
//案例2.随机生成1~100之间的数放在ArrayLiat中,要求这10个数不能重复,并且都是偶数(添加10次,可能循环很多次)
ArrayList arrLis = new ArrayList();
Random random = new Random();
int j = 0;
while (arrLis.Count<10)
{
//随机产生一个数字
int num = random.Next(1, 101);
if (num%2==0)
{
arrLis.Add(num);
}
j++;
}
Console.WriteLine("执行完毕,执行了{0}次",j);
Console.WriteLine("总数"+arrLis.Count);
for (int i = 0; i < arrLis.Count; i++)
{
Console.WriteLine(arrLis[i]+",");
}
#endregion
#region 案例3
Console.WriteLine("====================================================");
//案例3:有一个字符串是用空格分隔的一系列整数,写一个程序把其中的整数做如下重新排列打印出来:奇数显示在左,偶数在右。比如"2 7 8 3 22 9 5 11" 显示成"7 3 9 2 8 22 "
ArrayList arr1 = new ArrayList();
ArrayList arr2 = new ArrayList();
string str = "2 7 8 3 22 9 5 11";
string[] numbers = str.Split(' ');
for (int i = 0; i < numbers.Length; i++)
{
if (int.Parse(numbers[i]) % 2 == 0)
{
arr1.Add(numbers[i]);
}
else
{
arr2.Add(numbers[i]);
}
}
arr2.AddRange(arr1);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr2.Count; i++)
{
sb.Append(arr2[i] + " ");
}
Console.WriteLine(sb.ToString());
#endregion
Console.ReadKey();
}
}
public class person
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
}