11. C # Basic finishing (special collections and hash tables)

Source: Internet
Author: User

Special Collections: Queue, Stack one, stack stack class: Advanced, no index
New Stack ();

1. Add data: Push: Pushes elements into the collection

Ss. Push (3); SS. Push (5); SS. Push (7);

2. Get the data:
(1) Peek returns the object at the top of the stack without removing it (gets the value of the last entered element)

Console.WriteLine (ss. Peek ()); // 7

(2) Pop sets the elements together (read and remove)

Console.WriteLine (ss. Pop ()); // 7 Console.WriteLine (ss. Pop ()); // 5

3. Other operations

ToArray: Returns the array type, converts the elements in the stack to a collection, and then puts them in the array

Object [] Shuzu = (object[]) ss. ToArray (); foreach (object in Shuzu) {Console.WriteLine (a);}
Queuing Queue class: FIFO

1, the definition method:

New Queue ();

2. Increase:

(1) Enqueue put elements at the end of the queue

Q.enqueue (3); Q.enqueue (5); Q.enqueue (7);

(2) Peek: Returns the object at the beginning without removing it

Console.WriteLine ("Peek:"+q.peek ()); // 3

3. Get: Dequeue Remove and return the object at the beginning of the queue

Console.WriteLine ("Dequeue:"+q.dequeue ());
Hash table Hashtable Class

Reading data in pairs with key values, you can set the index yourself

 Hashtable ht = new   Hashtable (); Ht. ADD (  a  , "  zhangsan  ); //  an arbitrary type of key + an arbitrary type of value  ht. ADD ( "  ", "  lisi   ); Ht. ADD (  c  , "  wangwu  ); 

How to print the number of elements in a hash table:

ICollection Htkeys = ht. Keys; Console.WriteLine (Htkeys. Count);

Read key value

// CopyTo---Copied to an array string New string [3];htkeys. CopyTo (SS1,0);

Reading value values

ICollection htvalues = ht. Values; string New string [3];htvalues. CopyTo (SS2,0);

Paired reads: Both key and value need to be redefined

IDictionaryEnumerator ID = ht. GetEnumerator (); // Object Key1=id. Key; // gets a value // object value1 = ID. Value; // ID. MoveNext (); // moves down one element, returns a Boolean value, and can no longer be moved if False  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

11. C # Basic finishing (special collections and hash tables)

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.