C#집합의 HASHTABLE

11922 단어 Hashtable

1. HashTable 정의
System.Collections. Hashtable 클래스는 키/값 쌍의 집합을 나타낸다. 이 키/값은 키에 따라 해시 코드를 구성하고 모든 요소는 DictionaryEntry 대상에 저장된 키/값 쌍이다.키는 null일 수 없지만 값은 가능합니다.
2. 장점
1、Key를 통해 빠르게 찾습니다.
2.Hashtable은 라인이 안전합니다.
3. Hashtable의 구조기
구조기 함수
메모
Public  Hashtable ()
기본 초기 용량 (용량 크기 0), 마운트 인자, 해시 코드 제공 프로그램과 비교기를 사용하여Hashtable 클래스의 새로운 빈 실례를 초기화합니다.
public  Hashtable (IDictionary)
지정한 사전의 요소를 새 Hashtable 대상에 복사하여 Hashtable 클래스의 새로운 예를 초기화합니다.새 Hashtable 대상의 초기 용량은 복사된 요소 수와 같으며, 기본 불러오는 인자, 해시 코드를 사용하여 프로그램과 비교기를 제공합니다.
public  Hashtable (Int32)
지정한 초기 용량, 기본 불러오는 인자, 기본 해시 코드 제공 프로그램과 기본 비교기를 사용하여Hashtable 클래스의 새로운 빈 실례를 초기화합니다.
4.Hashtable의 속성
속성 이름
메모
Count
Hashtable에 포함된 키/값 쌍의 수를 가져옵니다.
IsFixedSize
Hashtable에 고정 크기가 있는지 여부를 나타내는 값을 가져옵니다.
IsReadOnly
Hashtable이 읽기 전용인지 여부를 나타내는 값을 가져옵니다.
Keys
Hashtable에 포함된 키ICollection를 가져옵니다.
Values
Hashtable의 값을 포함하는 ICollection을 가져옵니다.
5.Hashtable 방법
방법
메모
Void  Add (object key,object value)
지정된 키와 값이 있는 요소를 Hashtable에 추가합니다.
Void  Clear ()
Hashtable에서 모든 요소를 제거합니다.
Bool  Contains (object key)
Hashtable에 특정 키가 포함되어 있는지 확인합니다.
Bool  ContainsKey (object key)
Hashtable에 특정 키가 포함되어 있는지 확인합니다.
Bool  ContainsValue (object value)
Hashtable에 특정 값이 포함되어 있는지 여부를 결정합니다.
Void  Remove (object key)
Hashtable에서 지정된 키가 있는 요소를 제거합니다.
Void InsertRange(int index,Icollection collec)
목록 뒤에 있는 요소를 차례대로 뒤로 이동하여 지정된 위치부터 요소를 추가할 수 있습니다.
Clone ()
Hashtable의 얕은 테이블 복사본을 만듭니다.
GetObjectData ()
ISerializable 인터페이스를 실현하고 정렬화Hashtable에 필요한 데이터를 되돌려줍니다.
 
6. Hashtable의 사용 예
 
public class Program
{
        public static void Main(string[] args)
        {
            //    HashTable
            Hashtable openWith = new Hashtable();

            // HashTable    ,      key,        
            openWith.Add("txt", "notepad.exe");
            openWith.Add("bmp", "paint.exe");
            openWith.Add("dib", "paint.exe");
            openWith.Add("rtf", "wordpad.exe");

            

            //     key,     
            try
            {
                openWith.Add("txt", "winword.exe");
            }
            catch
            {
                Console.WriteLine("An element with Key = \"txt\" already exists.");
            }

            //  key    
            Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);

            //    
            openWith["rtf"] = "winword.exe";
            Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);

            //      ,       
            openWith["doc"] = "winword.exe";

            //  HashTable       ,     (          )
            //  (         ,            (  Visual Basic    Nothing),                 。 )
            try
            {
                Console.WriteLine("For key = \"tif\", value = {0}.", openWith["tif"]);
            }
            catch
            {
                Console.WriteLine("Key = \"tif\" is not found.");
            }

            //         key
            if (!openWith.ContainsKey("ht"))
            {
                openWith.Add("ht", "hypertrm.exe");
                Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
            }

            //  HashTable
            Console.WriteLine();
            foreach (DictionaryEntry de in openWith)
            {
                Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
            }

            //   HashTable     
            ICollection valueColl = openWith.Values;


            Console.WriteLine();
            foreach (string s in valueColl)
            {
                Console.WriteLine("Value = {0}", s);
            }

            //   HashTable     
            ICollection keyColl = openWith.Keys;


            Console.WriteLine();
            foreach (string s in keyColl)
            {
                Console.WriteLine("Key = {0}", s);
            }


            Console.WriteLine("
Remove(\"doc\")"); // openWith.Remove("doc"); if (!openWith.ContainsKey("doc")) { Console.WriteLine("Key \"doc\" is not found."); } Hashtable mySourceHT = new Hashtable(); mySourceHT.Add("A", "valueA"); mySourceHT.Add("B", "valueB"); // String[] myTargetArray = new String[15]; myTargetArray[0] = "The"; myTargetArray[1] = "quick"; myTargetArray[2] = "brown"; myTargetArray[3] = "fox"; myTargetArray[4] = "jumped"; myTargetArray[5] = "over"; myTargetArray[6] = "the"; myTargetArray[7] = "lazy"; myTargetArray[8] = "dog"; // Console.WriteLine("The target Array contains the following before:"); PrintValues(myTargetArray, ' '); // hashtable key Console.WriteLine("After copying the keys, starting at index 6:"); mySourceHT.Keys.CopyTo(myTargetArray, 6); PrintValues(myTargetArray, ' '); // hashtable Value Console.WriteLine("After copying the values, starting at index 6:"); mySourceHT.Values.CopyTo(myTargetArray, 6); PrintValues(myTargetArray, ' '); Console.Read(); } // public static void PrintValues(String[] myArr, char mySeparator) { for (int i = 0; i < myArr.Length; i++) Console.Write("{0}{1}", mySeparator, myArr[i]); Console.WriteLine(); } }
 
 
7、Hashtable 스트리밍 방법
방법 1
 foreach (System.Collections.DictionaryEntry objDE in objHasTab){    Console.WriteLine(objDE.Key.ToString());    Console.WriteLine(objDE.Value.ToString());}
 
방법 2
System.Collections.IDictionaryEnumerator enumerator = objHashTablet.GetEnumerator();while(enumerator.MoveNext(){Console.WriteLine(enumerator.Key);//Hashtable 켄타우로스Console.WriteLine
}
8.Hashtable 정렬
//ht의 키 객체를 모두 ArrayList로 복사
 ArrayList al = new ArrayList(ht.Keys);
 /*ht.Keys는 ht의 모든 키 대상으로 구성된 집합을 되돌려줍니다. 이 집합을 ArrayList 구조 방법에 전달하면 패키지를 얻을 수 있습니다.
* 모든 키 객체의 동적 배열
*/
  al.Sort();//작은 것부터 큰 것까지 배열하다
//정렬 완료 출력
   for (int i = 0; i < al.Count;i++ )
   {
          object e=al[i];
          object temp = (object)ht[e];//키를 색인으로 해서 대응하는 값 대상을 얻다
           Console.WriteLine(temp.tostring());
}

좋은 웹페이지 즐겨찾기