C # Learning ArrayList
In the process of learning C #, ArrayList is actually a dynamic array that can add, insert, and delete elements.
Operations such as clearing, sorting, and reversing can implement the ICollection and IList interfaces and flexibly set the array size. In this case, how does ArrayList expand and what methods does it have? Next we will introduce it.
First, you must manually add a reference namespace System. Collection before using ArrayList.
The following format is
ArrayList List = new ArrayList (); // create a new object and keep all attributes of ArrayList.
For (int I = 0; I <10, I ++) // Add 10 Int elements to the array.
The next step is how to process some ArrayList methods.
List. add (I); // use add to Add an array. In this case, you can use "subscript" (INDEX) to access data. The data must be of the object type and should be converted if necessary, lee's conversion principle must be met
List. RemoveAt (I); // remove element I
List. Insert (I, I + 1); // Insert a value to a position under the target
List. InsertRange (4, nums); // insert an array to an index position (that is, add a batch of elements from the specified position, and move the elements after the List in turn)
List. RemoveAt (I); // remove the value corresponding to the index
List. RemoveRange (3,100); // Number of removed indexes starting from the specified index. If the index is exceeded, an exception is reported.
List. Contains (I); // determines whether the collection Contains this element.
Int number = List. IndexOf (I); // you can specify the index corresponding to an element.
Int [] nums = [1, 2, 3];
List. Remove (I); // removes an element.
List. Reverse (); // Reverse
ArrayList. Sort (); // sorting, which can be Max, Min, Avg, etc.
Int num = List. Count; // number of elements in the Set
List. clear // clear all current elements
The above are some common methods of ArrayList, which should be used flexibly during use.