Integration
1) set interfaces and types
Collection classes can be combined into collections to store object-type Elements and generic collection classes. No generics existed before. NET 2.0. Currently, generic collection classes are usually the first type of the set. Generic collection classes are type-safe. If you use value types, you do not need to bind them. If you want to add different types of objects in the collection, and these objects are not mutually derived, for example, to add int and string objects in the collection, you only need to use the object-based collection class. Another set of collection classes is dedicated to specific types of collections. For example, the stringcollection class is dedicated to the string type.
2) Create list
Use the default constructor to create an empty list. After an element is added to the list, the capacity of the List is expanded to four acceptable elements. If 5th elements are added, the size of the List is reset to eight elements. If eight elements are not enough, the size of the List is reset to 16. Each time, the capacity of the List is reset to twice the original capacity.
Arraylist objectlist = new arraylist ();
List <int> intlist = new list <int> ();
List <racer> racers = new list <racer> ();
If the capacity of the list changes, the entire set will be re-allocated to a new memory block. In the implementation code of list <t>, a T-type array is used. By re-allocating memory, create a new array. array. Copy () copies the elements in the old array to the new array. To save time, if you know the number of elements in the list in advance, you can use the constructor to define its capacity. A set of 10 elements is created below. If the capacity is insufficient to accommodate the elements to be added, set the size of the set to 20 or 40 again, which is twice the original size each time.
Arraylist objectlist = new arraylist (10 );
List <int> intlist = new list <int> (10 );
You can use the capacity attribute to obtain and set the capacity of a set.
Objectlist. capacity = 20;
Intlist. capacity = 20;
The capacity is different from the number of elements in the set. The number of elements in the collection can be read using the Count attribute. Of course, the capacity is always greater than or equal to the number of elements. As long as no element is added to the list, the number of elements is 0.
Console. writeline (intlist. Count );
If you have added an element to the list and do not want to add more elements, you can call the trimexcess () method to remove unnecessary capacity. However, repositioning takes time, so if the number of elements exceeds 90% of the capacity, the trimexcess () method will do nothing.
Intlist. trimexcess ();
1. Add Elements
You can use the add () method to add elements to the list, as shown below. The instantiated generic type defines the type of the first parameter using the add () method:
List <int> intlist = new list <int> ();
Intlist. Add (1 );
Intlist. Add (2 );
List <string> stringlist = new list <string> ();
Stringlist. Add ("one ");
Stringlist. Add ("two ");
The variable racers is defined as the type list <racer>. Use the new operator to create a new object of the same type. Because the class list <t> is instantiated using a specific class racer, only the racer object can be added using the add () method. In the following example, four first-level equation runners are created and added to the set.
List <racer> racers = new list <racer> (20 );
Racer Graham = new racer ("Graham", "Hill "," UK ", 14 );
Racers. Add (Graham );
Racer Emerson = new racer ("Emerson", "Fittipaldi "," Brazil ", 14 );
Racers. Add (Emerson );
Racer Mario = new racer ("Mario", "Andretti "," USA ", 12 );
Racers. Add (Mario );
Racers. Add (new racer ("Michael", "Schumacher "," Germany ", 91 ));
Racers. Add (new racer ("Mika", "Hakkinen "," Finland ", 20 ));
Use the addrange () method of the List <t> class to add multiple elements to the set at a time. The parameter of the addrange () method is an object of the ienumerable <t> type. Therefore, an array can be transmitted as follows:
Racers. addrange (new racer [] {
New racer ("Niki", "Lauda "," Austria ", 25)
New racer ("Alian", "Prost "," France ", 51 });
If you know the number of elements in the set when instantiating the list, you can also transmit any object that runs ienumerable <t> to the class constructor. This is very similar to the addrange () method:
List <racer> racers = new list <racer> (new racer [] {
New racer ("Jochen", "Rindt "," Austria ", 6)
New racer ("Ayrton", "Senna "," Brazil ", 41 });
2. insert element
You can use the insert () method to insert an element at a specified position:
Racers. insert (3, new racer ("Phil", "Hill "," USA ", 3 ));
The insertrange () method provides the capacity to insert a large number of elements, similar to the previous addrange () method.
If the index set is larger than the number of elements in the Set, an argumentoutofrangeexception type exception is thrown.
3. Access Element
All classes that run the ilist and ilist <t> interfaces provide an indexer. Therefore, you can use the indexer to transfer element numbers to access elements. The first element can be accessed with an index value of 0.
Racer R1 = racers [3];
You can use the Count attribute to determine the number of elements, then use the for loop to iterate each element in the Set, and use the indexer to access each item:
For (INT I = 0; I <racers. Count; I ++)
{
Console. writeline (racers [I]);
}
List <t> executes the ienumerable interface, so you can use the foreach statement to iterate the elements in the set.
Foreach (racer R in racers)
{
Console. writeline (R );
}
4. Delete Elements
When deleting an element, you can use the index or transmit the element to be deleted. The following code sends 3 to removeat () and deletes the 4th elements:
Racers. removeat (3 );
The removerange () method can delete many elements from the collection. Its first parameter specifies the element index to be deleted, and the second parameter specifies the number of elements to be deleted.
Int Index = 3;
Int COUNT = 5;
Racers. removerange (index, count)
You can use the removeall () method to delete all elements with specified features from the set. This method uses the predicate <t> parameter discussed in the following search for elements. To delete all elements in the set, you can use the clear () method defined by the icollection <t> interface.