Definition: objects can be collected together when they are created, managed and operated as a group, and one of them can be referenced separately when needed.
1: simple cluster-array
Traverse Arrays
For (int I = 0; I <= 99; I ++ ){
If (studentBody [I]! = Null) {// avoids mines
Console. WriteLine (studentBody [I]. GetName ());
}
}
2: ordered list
2.1 The ordered list is similar to the array, but the items stored in the list are placed in a specific order, and the values are obtained in this order. When a new item is inserted, the length is automatically added to the ordered list. (ArrayList)
Using System;
Using System. Collections;
Public class SamplesArrayList {
Public static void Main (){
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList ();
MyAL. Add ("Hello ");
MyAL. Add ("World ");
MyAL. Add ("! ");
// Displays the properties and values of the ArrayList.
Console. WriteLine ("myAL ");
Console. WriteLine ("Count: {0}", myAL. Count );
Console. WriteLine ("Capacity: {0}", myAL. Capacity );
Console. Write ("Values :");
PrintValues (myAL );
}
Public static void PrintValues (IEnumerable myList ){
Foreach (Object obj in myList)
Console. Write ("{0}", obj );
Console. WriteLine ();
}
}
/*
This code produces output similar to the following:
MyAL
Count: 3
Capacity: 4
Values: Hello World!
*/
2.2 classified ordered list (a special ordered list, which automatically adds newly added objects to the end of the list, in the sorted categories list, the newly added objects are inserted to the appropriate positions of the List to maintain the sorted categories of the list,
For a sorted list, we must define the sort basis for objects, that is, define a category key. In typical cases, SortedList. A set of key-value pairs. These key-value pairs are sorted by keys and can be accessed by keys and indexes .)
Using System;
Using System. Collections;
Public class SamplesSortedList {
Public static void Main (){
// Creates and initializes a new SortedList.
SortedList mySL = new SortedList ();
MySL. Add ("Third ","! ");
MySL. Add ("Second", "World ");
MySL. Add ("First", "Hello ");
// Displays the properties and values of the SortedList.
Console. WriteLine ("mySL ");
Console. WriteLine ("Count: {0}", mySL. Count );
Console. WriteLine ("Capacity: {0}", mySL. Capacity );
Console. WriteLine ("Keys and Values :");
PrintKeysAndValues (mySL );
}
Public static void PrintKeysAndValues (SortedList myList ){
Console. WriteLine ("\ t-KEY-\ t-VALUE -");
For (int I = 0; I <myList. Count; I ++ ){
Console. WriteLine ("\ t {0 }:\ t {1}", myList. GetKey (I), myList. GetByIndex (I ));
}
Console. WriteLine ();
}
}
/*
This code produces the following output.
MySL
Count: 3
Capacity: 16
Keys and Values:
-KEY--VALUE-
First: Hello
Second: World
Third :!
*/
3: Set (it is an unordered cluster, that is, it cannot be accessed through the location index after the new item is inserted into the set) note that duplicate object references are not allowed in the collection, but the same object can be added multiple times to the ordered list.
4: Dictionary (a typical Hashtable is to store a unique query key when an object reference is stored)
Using System;
Using System. Collections;
Class Example
{
Public static void Main ()
{
// Create a new hash table.
//
Hashtable openWith = new Hashtable ();
// Add some elements to the hash table. There are no
// Duplicate keys, but some of the values are duplicates.
OpenWith. Add ("txt", "notepad.exe ");
OpenWith. Add ("bmp", "paint.exe ");
OpenWith. Add ("dib", "paint.exe ");
OpenWith. Add ("rtf", "wordpad.exe ");
// The Add method throws an exception if the new key is
// Already in the hash table.
Try
{
OpenWith. Add ("txt", "winword.exe ");
}
Catch
{
Console. WriteLine ("An element with Key = \" txt \ "already exists .");
}
// The Item property is the default property, so you
// Can omit its name when accessing elements.
Console. WriteLine ("For key = \" rtf \ ", value = {0}.", openWith ["rtf"]);
// The default Item property can be used to change the value
// Associated with a key.
OpenWith ["rtf"] = "winword.exe ";
Console. WriteLine ("For key = \" rtf \ ", value = {0}.", openWith ["rtf"]);
// If a key does not exist, setting the default Item property
// For that key adds a new key/value pair.
OpenWith ["doc"] = "winword.exe ";
// ContainsKey can be used to test keys before inserting
// Them.
If (! OpenWith. ContainsKey ("ht "))
{
OpenWith. Add ("ht", "hypertrm.exe ");
Console. WriteLine ("Value added for key = \" ht \ ": {0}", openWith ["ht"]);
}
// When you use foreach to enumerate hash table elements,
// The elements are retrieved as KeyValuePair objects.
Console. WriteLine ();
Foreach (DictionaryEntry de in openWith)
{
Console. WriteLine ("Key = {0}, Value = {1}", de. Key, de. Value );
}
// To get the values alone, use the Values property.
ICollection valueColl = openWith. Values;
// The elements of the ValueCollection are stronugly typed
// With the type that was specified for hash table values.
Console. WriteLine ();
Foreach (string s in valueColl)
{
Console. WriteLine ("Value = {0}", s );
}
// To get the keys alone, use the Keys property.
ICollection keyColl = openWith. Keys;
// The elements of the KeyCollection are stronugly typed
// With the type that was specified for hash table keys.
Console. WriteLine ();
Foreach (string s in keyColl)
{
Console. WriteLine ("Key = {0}", s );
}
// Use the Remove method to remove a key/value pair.
Console. WriteLine ("\ nRemove (\" doc \")");
OpenWith. Remove ("doc ");
If (! OpenWith. ContainsKey ("doc "))
{
Console. WriteLine ("Key \" doc \ "is not found .");
}
}
}
/* This code example produces the following output:
An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Value added for key = "ht": hypertrm.exe
Key = dib, Value = paint.exe
Key = txt, Value = notepad.exe
Key = ht, Value = hypertrm.exe
Key = bmp, Value = paint.exe
Key = rtf, Value = winword.exe
Key = doc, Value = winword.exe
Value = paint.exe
Value = notepad.exe
Value = hypertrm.exe
Value = paint.exe
Value = winword.exe
Value = winword.exe
Key = dib
Key = txt
Key = ht
Key = bmp
Key = rtf
Key = doc
Remove ("doc ")
Key "doc" is not found.
*/
From yikeshu