1. Describe the collection?
0.1 collections are different from arrays, and are a set of mutable types of elements that may share certain characteristics that need to be manipulated in some way. In general, the types of these elements are the same for ease of operation
0.2 the difference between a set and an array: An array is a contiguous area of data of the same type, and the collection can be discontinuous, with multiple data types
0.3 the foreach () in the collection is also applicable
0.4 definition of the set: ArrayList al = new ArrayList (); Defines a collection, which is a class that, in the using System.Collections Library, requires a reference
0.5 assignment of the set: double Fenshu = 0;
0.6 inserting data into the collection: Al.insert (,); Before the comma is the index number, after the comma is the data (when there are three data in the collection, the index number is inserted at 1 o'clock, the original 1 index number of data will be 2, followed by backward one bit)
0.7 removing data from the collection: Al. Remove ();//parentheses fill in the data to be removed in the collection (if there are two duplicate numbers in the collection in the removal.) Remove () removes only the first occurrence of the number)
0.8count;//View the length of the collection, return int type
0.9 sorting in the collection:. Sort ();//This is sorted in ascending order, descending order in ascending order after the method is flipped (flipped ———. Reverse ();)
0.10 find the index number of the element in the collection: (Be sure to note whether the data type matches.) If the return value is-1, then the index number of the element is not found)
int s = al. IndexOf (); In parentheses is the element to be found, the first occurrence of the index number
int S1 = al. LastIndexOf (); In parentheses is the element to be found, the index number of the last occurrence of this element
0.11 emptying the collection:. Clear ();
0.12 gets the number of elements within the collection: Console.WriteLine (at. count);//number of output sets
0.13 copy the element data from the collection and load it into the new collection:
ArrayList xal = new ArrayList ();
Xal = (ArrayList) al. Clone ();
0.14 · Determine if a collection contains this element data returns a bool value:
BOOL B = al. Contains ();//in parentheses for the element to find whether the collection contains
————— Special Collection: Stack, Queue, hash table (Hashtable)
Stack heap meaning, advanced out, LIFO (heap no index)
1 • Build the Stack s=new. Stack ();
2 · Assignment: S. Push (1); Pushing data into the heap
3 • Output: Console.WriteLine (S.pop ());
4 • Clear the collection:. Clear ();
5 · String Tanchu = S.peek (). ToString ();//Only get the last value that goes in, not remove
String Tanchu = S.pop (). ToString ();//pop is the element that pops up and removes the last entry.
6 · Stack Fuzhi = (stack) s.clone ();//Assignment Set
7 · Console.WriteLine (s.count);//Gets the number of elements in the collection
Queue FIFO first, backward after
1 · Build: Queue q = new Queue ();
2 int chu = Int. Parse (Q.dequeue (). ToString ());//Gets the first entry in the element and removes it from the collection
3 int zhi = Int. Parse (Q.peek (). ToString ());//Read the first in element, not remove
4 bool D = Q.contains (5);//See if the collection contains an element in parentheses, return a bool value
Hash table (Hashtable) advanced out, LIFO a position contains two values (,) preceded by an index is an element
1 • Build Hashtable ht = new Hashtable ();
2 · Ht. ADD (0, "AA"); To add a key value to a hash table
3 ht. Remove (4); Removed by the keys value in parentheses
4 · Console.WriteLine (HT. Contains (4));//Determine if a key is included
5 · Output
foreach (int i in HT. Keys)//keys represents the index
{
Console.WriteLine (i); Advanced post-out, LIFO
}
foreach (int i in HT: Values)//. Values represents an element
{
Console.WriteLine (i); Advanced post-out, LIFO
}
What if you want to output both the index and the element?
Using enumerations to output index numbers and elements
IDictionaryEnumerator IDE = ht. GetEnumerator ();
while (IDE. MoveNext ())
{
Console.WriteLine (IDE. Key + "" +ide. Value);
}
Convert a hash table into ArrayList
ArrayList al = new ArrayList ();
foreach (String J in Ht. Values)//values represents an element in a hash table
{
Al. Add (j);
}
Generics and generic collections
Generic type
Definitions and references:
Generics are classes, interfaces, structures, or methods that have placeholders. It differs from the normal class in that one or more placeholders for the representation type are represented by angle brackets
Generic Collection
Collection
The advantages of arrays: continuous storage in memory, fast and easy to iterate through elements, and quickly modify elements, etc.
Disadvantage: You must specify the size of the array variable at creation time, and it is difficult to add elements between the two elements.
The difference between generics and generic collections
In both Java and C #, all types are based on the underlying type of object. You can think of all other types as generics of object.
A generic collection refers to the collection of these generics together, but it must be selected before use to add a type.
If list<t> is a generic collection, you can put all the generics in that collection. However, you must choose the type of the declaration T, otherwise you cannot put it. This involves the storage of the problem, if we were for some sets, such as we can declare such an array int[] a = new int[10]; once declared, its size is immutable, if we do not know how many to deposit, assuming that 10 int is stored, you use int[] A = new INT[10]; Of course, there is no objection, but the problem is that as the program runs we can ask for less than 10, and this array cannot be extended, or you might say, "int[] a = new int[1000]; Sure enough, but there are plenty of inner spaces to waste! So we consider another type of array which is very good, but because the array is put in to be packed, removed when unpacking, greatly wasting CPU utilization.
So, in. NET framework2.0, the support for generics was added, that is, the IList interface is implemented, he has both the feature of the array and the characteristics of the length can be increased to add small, or to avoid packaging problems, which is also after the. NET 2.0 Microsoft said, No post-mortem we all use ArrayList and so on to support the cause of generics!
In fact, because of long-term habits, generics refer to the collection type that implements an array of IList interfaces, so when we call generics or generic sets, we are actually the same concept!
Generic classes are not commonly used to do more introduction
Use collections to organize related data