Array and set
Foreach
Format: foreach (type name I in array name)
Every element in the array can be iterated cyclically, and the elements in the array can be placed in variable I, avoiding the danger of accessing illegal elements.
Comparison of foreach and
Foreach traverses the entire array. For can traverse parts of the array.
Foreach always traverses from index 0 to array length minus 1, and cannot reverse traversal. For can reverse Traversal
If you need to know that the element index is not the element value, you should use
To modify the value of an element, you must use for. The variable for foreach iteration must be a read-only copy of each element.
Multi-dimensional array format: <type Name> [,] an array name with a comma represents 2 as an array, 2 represents a three-dimensional array, and so on
Set:
You can use the array class and systems. Collections class to add, delete, and modify an individual element or a range of elements in the set.
Arraylist class
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. collections;
Namespace five
{
Class Program
{
Static void main (string [] ARGs)
{
Arraylist list = new arraylist (10 );
For (INT I = 0; I <10; I ++ ){
List. Add (I );
}
List. insert (5, 90 );
Console. writeline (list. Count );
For (INT I = 0; I <list. Count; I ++)
{
Int number = (INT) list [I];
Console. Write (number + "");
}
Console. Readline ();
}
}
}
The queue class first-in-first-out collection class implements the iconllection interface.
You can perform the following operations on the queue and its elements:
Enqueue: add an element to the end of the queue,
Dequeue: removes the first entry element from the queue header,
Peek: returns the first entry element from the beginning of the queue class, but does not delete it from the queue.
Sing system;
Using system. Collections. Generic;
Using system. text;
Using system. collections;
Namespace five
{
Class Program
{
Static void main (string [] ARGs)
{
Queue queue = new Queue ();
Queue. enqueue (2 );
Queue. enqueue (21 );
Queue. enqueue (212 );
Queue. enqueue (2123 );
Foreach (int I in queue ){
Console. writeline (I );
}
Console. writeline ();
Console. writeline (queue. Peek ());
Console. writeline ();
Foreach (int I in queue)
{
Console. writeline (I );
}
Console. writeline ();
Queue. dequeue ();
Foreach (int I in queue)
{
Console. writeline (I );
}
Console. Readline ();
}
}
}
Stack class (first-in-first-out)
Push: insert element at the top of the stack
Pop: deleting elements at the top of the stack
Peek: The top element of the stack is returned but not deleted.
Hashtable class
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. collections;
Namespace five
{
Class Program
{
Static void main (string [] ARGs)
{
Hashtable table = new hashtable ();
Table. Add ("zhangsan", 34); // If the table contains the key = zhangsan element, an exception is thrown.
Table ["Lisi"] = 90;
Foreach (dictionaryentry element in table)
{// Use the iterator to generate a dictionaryentry object, including the key/value pair
String name = (string) element. Key;
Int score = (INT) element. value;
Console. writeline ("{0}'s score is {1}:", name, score );
}
Console. Readline ();
}
}
}
The sortedlist class is similar to the hashtable class. The difference is that sortedlist is always sorted by key.