Address: http://blog.csdn.net/loveandangle/article/details/6733773
- /* Generic class: queue
- * Namespace: system. Collections. Generic
- * Description: the object's first-in-first-out set.
- * Type parameter: t -- specifies the type of the element in the queue.
- * Note:
- * Items in the queue container can only be deleted from the beginning, but cannot be deleted without rules. For example, you cannot directly Delete 2nd or the last item,
- * To delete 2nd items, you can only Delete the first item before deleting the first item. (only the first item can be deleted !!!)
- * Queue is very swimming in receiving ordered storage messages for sequential processing. objects stored in the queue are inserted at one end and removed from the other end.
- * The capacity of the queue is the number of elements that can be stored by the queue. The default initial capacity is 8.
- * When an element is added to the queue, the internal array is reassigned and the capacity is automatically increased as needed. You can use trimexcess to reduce the capacity.
- * Queue accepts null references as the valid values of the reference type and allows repeated elements.
- */
- // Example:
- // Example 1
- // Create a string queue with the default capacity and use the enqueue method to queue the string.
- // Enumerate queue elements, which does not change the queue status.
- // Use the dequeue method to column the first string, use the peek method to find the next item in the queue, and then use the dequeue method to column it.
- // Use the toarray method to create an array object and copy the queue elements to the array.
- // Create an array that is twice the queue size and use the copyto method to copy array elements from the array.
- // Use the contains method to display the string "four" in the first queue copy, then use the clear method to clear the copy, and the count attribute shows that the queue is empty.
- Using system;
- Using system. Collections. Generic;
- Class Program
- {
- Static void main (string [] ARGs)
- {
- // (1) create a queue container object and queue it to the container
- Queue <string> numbers = new queue <string> ();
- Numbers. enqueue ("one ");
- Numbers. enqueue ("two ");
- Numbers. enqueue ("three ");
- Numbers. enqueue ("four ");
- Numbers. enqueue ("five ");
- // (2) enumerate queue Elements
- Foreach (string s in numbers)
- {
- Console. writeline (s );
- }
- // (3) use the dequeue method/return the queue header object and remove it. Use the peek method to return the queue header object but do not remove it. Then use the dequeue method to make it out of the column.
- Console. writeline ("\ ndequeuing '{0}'", numbers. dequeue (); // return the queue header object and remove it
- Console. writeline ("Peek at next item to dequene: {0}", numbers. Peek (); // returns the queue header object but does not remove it
- Console. writeline ("dequeuing '{0}'", numbers. dequeue ());
- // (4) use the toarray method to create an array object and copy the queue elements to the array.
- String [] strarray = numbers. toarray ();
- Console. writeline ("\ n Array object :");
- Foreach (string s in strarray)
- {
- Console. writeline (s );
- }
- // (5) create a queue container from an array object
- Queue <string> queuecopy = new queue <string> (strarray );
- Console. writeline ("\ n queue container created by array object \" strarray :");
- Foreach (string s in queuecopy)
- {
- Console. writeline (s );
- }
- // (5) Create an array that is twice the queue size and use the copyto method to copy array elements from the array.
- String [] array2 = new string [numbers. Count * 2];
- Numbers. copyto (array2, 0); // copy all contents to the array object starting from the index 0 of the queue container
- Queue <string> queuecopy2 = new queue <string> (array2 );
- Console. writeline ("\ n queue container created by array object \" array2 :");
- Foreach (string s in queuecopy2)
- {
- Console. writeline (s );
- }
- // (6) use the contains method to display the string "four" in the first queue copy, then clear the copy using the clear method, and the queue is empty by the count attribute.
- Console. writeline ("\ nqueuecopy. contains (\ "four \") = {0} ", queuecopy. contains ("four"); // determines whether an element is in the queue
- Console. writeline ("number of elements before queuecopy is cleared:" + queuecopy. Count );
- Queuecopy. Clear ();
- Console. writeline ("number of elements after queuecopy is cleared:" + queuecopy. Count );
- Console. Read ();
- }
- }