11. C # basic sorting (Special set and hash table ),
Special set: queue, Stack 1, Stack class: first-in-first-out, no index
Stack ss = new Stack();
1. Add data: push elements into the set
ss.Push(3);ss.Push(5);ss.Push(7);
2. obtain data:
(1) peek returns the object at the top of the stack but does not remove it (gets the value of the last element to enter)
Console.WriteLine(ss.Peek());//7
(2) pop pops up the elements one by one (read and remove)
Console.WriteLine(ss.Pop());//7Console.WriteLine(ss.Pop());//5
3. Other operations
ToArray: returns the array type, converts the elements in the stack to a set, and then places them in the array.
object[] shuzu = (object[])ss.ToArray();foreach(object a in shuzu){Console.WriteLine(a);}
Ii. Queue: FIFO
1. Definition method:
Queue q = new Queue();
2. Add:
(1) Enqueue puts the element at the end of the queue
q.Enqueue(3);q.Enqueue(5);q.Enqueue(7);
(2) Peek: the start object is returned but not removed.
Console.WriteLine("Peek:"+q.Peek());//3
3. Get: Remove Dequeue and return the object at the beginning of the queue
Console.WriteLine("Dequeue:"+q.Dequeue());
Hashtable class
You can set indexes to read data in pairs with key values.
Hashtable ht = new Hashtable (); ht. add ("a", "zhangsan"); // a key of any type + a value of any type ht. add ("B", "lisi"); ht. add ("c", "wangwu ");
To print the number of elements in a hash table:
ICollection htkeys = ht.Keys;Console.WriteLine(htkeys.Count);
Read key value
// Copyto --- copy to an array string [] ss1 = new string [3]; htkeys. CopyTo (ss1, 0 );
Read value
ICollection htvalues = ht.Values;string[] ss2 = new string[3];htvalues.CopyTo(ss2,0);
Read in pairs: key and value must be redefined.
IDictionaryEnumerator id = ht. getEnumerator (); // object key1 = id. key; // get a value // object value1 = id. value; // id. moveNext (); // move down an element and return a Boolean value. If it is false, the while (id. moveNext () {object key2 = id. key; Console. writeLine (key2.ToString (); object value2 = id. value; Console. writeLine (value2.ToString ());}
Ht. Remove ("B"); -- Remove the key based on the key value