ArrayList are very similar to arrays, others call it an array list, ArrayList can be dynamically maintained
Tips:
Like arrays, the data stored in ArrayList is called an element, and the number of elements ArrayList can hold is ArrayList capacity, with a default initial capacity of 0, which can be indexed to the elements in the ArrayList, and the index starts at 0
ArrayList belongs to the System.Collections namespace and is a collection of
Using System.Collection
ArrayList arr=new ArrayList ();
ArrayList common methods and properties
Property name Description
Count gets the number of elements actually contained in the ArrayList
Return value type method name Description
The int add (object value) adds the object at the end of the ArrayList
void RemoveAt (int index) removes the element arraylist at the specified index
void remove (object value) removes a specific object from the ArrayList
void Clear () removes all elements from the ArrayList
Add data to ArrayList
1.ArrayList adding elements through the Add method
Return value: The value is an int type that is used to return the index of the element
Parameters: If the element added to ArrayList is a value type, the elements will be boxed into an object reference type and then saved, so all elements in ArrayList are references to objects
Accessing a single element in the ArrayList
ArrayList gets an element in the same way as the array, which is also accessed by index, the index of the first element in ArrayList is 0
Traversing elements in a ArrayList
Int[] Array=new int[]{1,2,3,4,5};
for (int i=0;i<array.length;i++) {
Console.WriteLine (Array[i]);
}
Delete an element in a ArrayList
ArrayList engineers=new ArrayList ();
Engineers. RemoveAt (0);
Enginneers. Remove (EMA);
MessageBox.Show (String. Format ("the department consists of {0} engineers", engineers. Count.tostring ()));
Se leave= (SE) engineers[0];
Messagebos.show (leave. Sayhi ());
The RemoveAt () and remove () methods can only delete one element
The clear () method clears the elements in the ArrayList
ArrayList in C #