The System. Collections namespace contains interfaces and classes that define a set of objects (such as lists, queues, bit arrays, hash tables, and dictionaries.
System. collections. the Generic namespace contains interfaces and classes that define a Generic set. A Generic set allows you to create a strongly typed set. It provides better type security and performance than a non-Generic strong set.
The System. Collections. Specialized namespace contains Specialized and strong Collections, such as list dictionaries for links, bitvectors, and Collections that only contain strings.
(1) ArrayList class: use an array dynamically increased by size as needed.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Collections;
Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{
ArrayList al = new ArrayList ();
Al. Add (100); // Add a single
Foreach (int number in new int [6] {9, 3, 7, 2, 4, 8 })
{
Al. Add (number); // Add group method 1
}
Int [] number2 = new int [2] {11, 12 };
Al. AddRange (number2); // method 2 for adding a group
Al. Remove (3); // Remove 3
Al. RemoveAt (3); // remove 3rd
ArrayList al = new ArrayList (al. GetRange (1, 3); // The new ArrayList takes only one portion of the old ArrayList.
Console. WriteLine ("Traversal method 1 :");
Foreach (int I in al) // do not force convert
{
Console. WriteLine (I); // method 1
}
Console. WriteLine ("Traversal method 2 :");
For (int I = 0; I <al2.Count; I ++) // The array is length
{
Int number = (int) al [I]; // be sure to force convert
Console. WriteLine (number); // method 2
}
}
}
}
(2) Queue: Queue, indicating the object's first-in-first-out set. The Enqueue method is used to enter the queue, and the Dequeue method is used to output the queue.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Collections;
Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{
Queue qu = new Queue ();
Queue qu2 = new Queue ();
Foreach (int I in new int [4] {1, 2, 3, 4 })
{
Qu. Enqueue (I); // enter the queue
Qu2.Enqueue (I );
}
Foreach (int I in qu)
{
Console. WriteLine (I); // traverse
}
Qu. Dequeue (); // team out
Console. WriteLine ("Dequeue ");
Foreach (int I in qu)
{
Console. WriteLine (I );
}
Qu2.Peek (); // returns the object at the beginning of the Queue without removing it.
Console. WriteLine ("Peek ");
Foreach (int I in qu2)
{
Console. WriteLine (I );
}
}
}
}
(3) Stack: A simple post-first-out non-generic set of objects. Push method into the stack, Pop method out of the stack.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Collections;
Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{
Stack sk = new Stack ();
Stack sk2 = new Stack ();
Foreach (int I in new int [4] {1, 2, 3, 4 })
{
Sk. Push (I); // inbound Stack
Sk2.Push (I );
}
Foreach (int I in sk)
{
Console. WriteLine (I); // traverse
}
Sk. Pop (); // output Stack
Console. WriteLine ("Pop ");