In the previous section, when we introduced the data structure of array, we said that it was static, the number of elements per dimension was compiled, and it was uniquely determined, and its type was strongly typed.
So, in this section, we'll talk about another data structure similar to array but overcomes the disadvantage of array:ArrayList.
First of all, it is not static, at compile time the number of elements of each dimension is not specified, the system default element number is 16, when the element increases and is about to be greater than 16 o'clock, it will increase the expansion to 32, in order to increase the regular growth, change the hours, the opposite processing.
Second, the element type is a weak type, object. At run time, depending on the type that is actually given, the type of each element is determined, that is, the elements in this set can be a variety of different elements, mixed-style.
Look first. NET provides the ArrayList interface:
1) object creation and initialization
// Object creation
ArrayList arrayList = new ArrayList () {3.14, "vuefine"};
// Add elements
arrayList.Add ("Hello wolrd");
arrayList.Add (5);
2) Access element
// Access element
object ele0 = arrayList [0];
Type t0 = ele0.GetType (); // double
3) Modify elements
// Modify the element
arrayList [0] = "Ni hao";
4) Delete elements
// Delete element
// Remove existing objects
arrayList.Remove (5);
arrayList.RemoveAt (0); // Remove non-existent objects
arrayList.Remove (12); // Do not throw exception
5) ArrayList and other object relationships
object cloneAL = arrayList.Clone (); // Create a shallow copy
Type tClone = cloneAL.GetType (); // Array is abstract, array can only be created by static method
Array array = Array.CreateInstance (typeof (object), arrayList.Count);
arrayList.CopyTo (array); // Copy to array
related articles:
js implements ArrayList function with example code
PHP implementation of C # cottage ArrayList
ArrayList sample code analysis of Java collection