11. C # basic sorting (Special set and hash table ),

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.