【學習筆記】Hashtable幾種常用的遍歷方法


Hashtable

在System.Collection是命名空間李Hashtable是程序員經常用到的類,它以快速檢索著稱,是研發人員開發當中不可缺少的利器。

Hashtable表示鍵/值對的集合,這些鍵/值對根據鍵的哈希代碼進行組織。Hashtable的鍵必須是唯一的,沒有有效的排序,他進行的是內在的排序。

Hashtable有以下4中遍歷方式

              1、以string對象為鍵值遍歷哈希表。

              2、以自定義對象為鍵值遍歷哈希表。

              3、以DictionaryEntry對象為鍵值遍歷哈希表。

              4、通過繼承IDictionaryEnumerator接口的對象來遍歷哈希表。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        class Person
        {
            private int age;
            public int Age
            {
                get { return age; }
                set { age = value; }
            }
            private string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }
            private string email;
            public string Email
            {
                get { return email; }
                set { email = value; }
            }
        }
        static void Main(string[] args)
        {
            var a = new Person
            {
                Age = 34,
                Name = "Jacky",
                Email = "Jacky@gmail.com"
            };

            var b = new Person
            {
                Age = 23,
                Name = "Ajay",
                Email = "Ajay@gmail.com"
            };

            var c = new Person
            {
                Age = 12,
                Name = "Bill",
                Email = "Bill@gmail.com"
            };

            var d = new Person
            {
                Age = 23,
                Name = "Gace",
                Email = "Gace@gmail.com"
            };

            var e = new Person
            {
                Age = 45,
                Name = "Jim",
                Email = "Jim@gmail.com"
            };
            var ht = new Hashtable
            {
                { "1", a },
                { "2", b },
                { "3", c },
                { "4", d },
                { "5", e }
            };

            Console.WriteLine("請輸入你的查詢的用戶名:");
            var strName = Console.ReadLine();

            //第一種方法
            foreach (string item in ht.Keys)
            {
                var p = (Person)ht[item];
                if (strName == p.Name)
                {
                    Console.WriteLine("查詢后的結果是:" + p.Name + "\t" + p.Email + "\t" + p.Age);
                }
            }
            Console.WriteLine("華麗的分割線=========================================================");
            //第二種方法
            foreach (Person item in ht.Values)
            {
                if (item.Name == strName)
                {
                    Console.WriteLine("查詢后的結果是:" + item.Name + "\t" + item.Email + "\t" + item.Age);
                }
            }
            Console.WriteLine("華麗的分割線=========================================================");
            //第三種方法
            foreach (DictionaryEntry item in ht)
            {
                if (strName == ((Person)item.Value).Name)
                {
                    Console.WriteLine("查詢后的結果是:" + ((Person)item.Value).Name + "\t" + ((Person)item.Value).Email + "\t" + ((Person)item.Value).Age);
                }
            }
            Console.WriteLine("華麗的分割線=========================================================");
            //第四種方法
            IDictionaryEnumerator id = ht.GetEnumerator();
            while (id.MoveNext())
            {
                Person p = (Person)ht[id.Key];
                if (p.Name == strName)
                {
                    Console.WriteLine("查詢后的結果是:" + p.Name + "\t" + p.Email + "\t" + p.Age);
                }
            }
            Console.ReadKey();
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM