Class is a special array. By adding and deleting elements, You can dynamically change the length of the array.
I. Advantages
1. Supports automatic resizing.
2. Flexible element insertion
3. Flexible deletion of Elements
4. flexible access to elements
Ii. Limitations
Compared with the average array, the speed is lower.
3. Add Elements
1. Public Virtual int add (object value );
Add the object to the end of arraylist
Arraylist alist = new arraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Content is ABCDE
2. Public Virtual void insert (INT index, object value );
Insert the element to the specified index of arraylist
Arraylist alist = new arraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Alist. insert (0, "AA ");
The result is aaabcde.
3. Public Virtual void insertrange (INT index, icollectionc );
Insert an element in the set to the specified index of arraylist.
Arraylist alist = new arraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Arraylist list2 = new arraylist ();
List2.add ("TT ");
List2.add ("TTT ");
Alist. insertrange (2, list2 );
The result is abtttttcde.
Iv. Delete
A) Public Virtual void remove (Object OBJ );
Remove the first matching item of a specific object from the arraylist. Note that it is the first
Arraylist alist = new arraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Alist. Remove ("");
The result is bcde.
2. Public Virtual void removeat (intindex );
Removes the element at the specified index of arraylist.
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Alist. removeat (0 );
The result is bcde.
3. Public Virtual void removerange (INT index, int count );
Remove a certain range of elements from the arraylist. Index indicates the index, and count indicates the number starting from the index.
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Alist. removerange (1, 3 );
The result is AE.
4. Public Virtual void clear ();
Remove all elements from arraylist.
5. Sorting
A) Public Virtual void sort ();
Sorts the elements in an arraylist or a part of it.
Arraylistalist = newarraylist ();
Alist. Add ("e ");
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Dropdownlist1.datasource = alist; // dropdownlistdropdownlist1;
Dropdownlist1.databind ();
The result is eabcd.
Arraylist alist = new arraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Alist. Sort (); // sort
Dropdownlist1.datasource = alist; // dropdownlistdropdownlist1;
Dropdownlist1.databind ();
The result is ABCDE.
B) Public Virtual void reverse ();
Reverses the order of elements in an arraylist or a part of it.
Arraylist alist = new arraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Alist. Reverse (); // reverse
Dropdownlist1.datasource = alist; // dropdownlistdropdownlist1;
Dropdownlist1.databind ();
The result is edcba.
6. Search
A) Public Virtual int indexof (object );
B) Public Virtual int indexof (object, INT );
C) Public Virtual intindexof (object, Int, INT );
Returns the index starting from scratch for the first matching item of a value in the arraylist or part of it. -1 is not found.
Arraylist alist = new arraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e ");
Intnindex = alist. indexof ("A"); // 1
Nindex = alist. indexof ("P"); // not found,-1
D) Public Virtual int lastindexof (object );
E) Public Virtual intlastindexof (object, INT );
F) Public Virtual intlastindexof (object, Int, INT );
Returns the index starting from scratch for the last matching item of a value in the arraylist or part of it.
Arraylist alist = new arraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("A"); // same as 0
Alist. Add ("D ");
Alist. Add ("e ");
Intnindex = alist. lastindexof ("A"); // The value is 2 instead of 0.
G) Public Virtual bool contains (objectitem );
Determines whether an element is in the arraylist. True is returned for inclusion; otherwise, false is returned.
7. Obtain the elements in the array
The following example uses an integer to obtain the value of an element.
Arraylist alist = new arraylist ();
For (INT I = 0; I <10; I ++)
Alist. Add (I );
For (I = 0; I <10; I ++)
Textbox1.text + = (INT) alist [I] + ""; // The method of obtaining the object is basically the same as that of a General array.
Result: 0 1 2 3 45 6 7 8 9
VIII. Others
1. Public Virtual intcapacity {Get; set ;}
Gets or sets the number of elements that an arraylist can contain.
2. Public Virtual intcount {Get ;}
Obtains the number of actually contained elements in the arraylist.
Capacity is the number of elements that can be stored in arraylist. Count is the number of actually contained elements in the arraylist. Capacity is always greater than or equal to count. If the Count value exceeds the capacity when an element is added, the capacity of the List is doubled by automatically allocating an internal array.
If the capacity value is explicitly set, the internal array also needs to be reassigned to accommodate the specified capacity. If capacity is explicitly set to 0, the common language runtime sets it to the default capacity. The default capacity is 16.
After clear is called, count is 0, and capacity switches to the default capacity of 16 instead of 0.
3. Public Virtual void trimtosize ();
Set the capacity to the actual number of elements in the arraylist.
If new elements are not added to the list, this method can be used to minimize the memory system overhead of the list.
To completely clear all elements in the list, call the clear method before calling trimtosize. If this parameter is left blank, arraylist sets the capacity of arraylist to the default capacity instead of zero.
Arraylist alist = new arraylist ();
Alist. Add ("");
Alist. Add ("B ");
Alist. Add ("C ");
Alist. Add ("D ");
Alist. Add ("e"); // COUNT = 5, capacity = 16,
Alist. trimtosize (); // COUNT = capacity = 5;