ArrayList class
Use size to implement the IList interface on an array that needs to be dynamically added
namespaces: System.Collections
Assembly: mscorlib
Syntax:
Public class Arraylist:ilist, ICollection, IEnumerable, ICloneable
1.ArrayList adding elements
1.1 Arraylist.add Method:
syntax: public virtual int ADD (Object value)
joins the object at the end of the ArrayList
ArrayList Myal = new ArrayList ();
Myal.add ("Hello");
Myal.add (123);
myal.add ("!");
myal.add ("Wang");
It is important to note that ArrayList can accept elements of different data types!
ArrayList accepts a null reference as a valid value and agrees to repeat the element.
From the syntax of the Add method, you can see that the parameter type is object, so you need to do a boxing operation when you run Arraylist.add.
1.2 Arraylist.addrange Method:
add ICollection elements to the end of ArrayList
syntax: public virtual void AddRange (ICollection c)
ArrayList myAL0 = new ArrayList ();
Myal0.add ("the");
Myal0.add ("quick");
Myal0.add ("Brown");
Myal0.add ("Fox");
//Creates and initializes a new Queue.
Queue myqueue = new Queue ();
Myqueue.enqueue ("jumped");
myqueue.enqueue ("over");
Myqueue.enqueue ("the");
myqueue.enqueue ("lazy");
myqueue.enqueue ("dog");
Console.WriteLine ("The ArrayList initially contains the following:");//the quick brown Fox
Console.WriteLine ("The Queue initially contains the following:");//jumped over the lazy dog
Myal.addrange (myqueue);
Console.WriteLine ("The ArrayList now contains the following:"),//the quick brown fox jumped over the the lazy dog
2. Use the indexer to access the interview:
int i = (int) myal[1];
Be able to see the appeal statement carried out in the unpacking, Myal[1] Type is object. The reference type needs to be converted to a value type.
3.ArrayList Delete Element (remove, RemoveAt, RemoveRange)
3.1 Arraylist.remove Method
Remove the first occurrence of a particular object from ArrayList, and note that it is the first one (ArrayList agrees to repeat elements).
syntax: public virtual void Remove (Object obj)
Myal.remove (123);
run through the appeal code. The element below the removed element will be moved up to occupy the vacated position, i.e. myal[0] = "Hello", myal[1] = "!", myal[2] = "Wang"
Myal.remove (456);
After running the appeal code, assuming that ArrayList does not include the specified object, the ArrayList remains unchanged and no exception is thrown.
3.2 Arraylist.removeat Method
removes the element at the specified index of the ArrayList
syntax: public virtual void RemoveAt (int index)
myal.removeat (1);
run through the appeal code. The element below the removed element is moved up to occupy the vacated position, i.e. myal[0] = "Hello", myal[1] = "Wang"
if the index is less than 0 or greater than or equal to count, the ArgumentOutOfRangeException exception is thrown.
3.3 Arraylist.removerange Method
remove a range of elements from the ArrayList
syntax: public virtual void removerange (int index, int count)
myal.add ("WU");
Myal.add ("Zhang");
myal.add ("Liu");//At this time Myal for "Hello" "Wang" "WU" "Zhang" "Liu"
Myal.removerange (1, 2);
after running the appeal code, Myal "Hello" "Zhang" "Liu"
4.arraylist.capacity Property
Gets or sets the number of elements that ArrayList can include
syntax: public virtual int capacity{get; set;}
ability to reduce capacity by calling trimtosize or by setting the capacity property on display.
4.1 Arraylist.trimtosize Method
set capacity to the actual number of elements in ArrayList
to reset ArrayList to its initial state, call the clear method before calling TrimToSize. Fixed null ArrayList will set ArrayList capacity to default capacity
ArrayList myAL1 = new ArrayList ();
Myal1.add ("the");
Myal1.add ("quick");
Myal1.add ("Brown");
Myal1.add ("Fox");
Myal1.add ("jumped");
Console.WriteLine ("Count: {0}", Myal1.count);//5
Console.WriteLine ("Capacity: {0}", myal1.capacity);//16
myal1.trimtosize ();
Console.WriteLine ("Count: {0}", Myal1.count);//5
Console.WriteLine ("Capacity: {0}", myal1.capacity);//5
myal1.clear ();
Console.WriteLine ("Count: {0}", Myal1.count);//0
Console.WriteLine ("Capacity: {0}", myal1.capacity);//5
myal1.trimtosize ();
Console.WriteLine ("Count: {0}", Myal1.count);//0
Console.WriteLine ("Capacity: {0}", myal1.capacity);//16
4.2 Arraylist.clear Method
remove all elements from ArrayList
syntax: public virtual void Clear ()
Note: Count is set to zero, but capacity remains unchanged
5.arraylist.count Property
gets the number of elements actually included in the ArrayList
syntax: public virtual int count{get;}
differences with capacity:
Capacity is the number of elements that ArrayList can store.
Count is the actual number of elements in ArrayList.
Capacity is always greater than or equal to count.
assuming that count exceeds capacity when the element is added, the capacity is actively added by allocating the internal array again before copying the old elements and adding the new elements.
6.ArrayList inserting elements
6.1 Arraylist.insert Method
inserts an element at the specified index of the ArrayList
syntax: public virtual void Insert (int index, Object value);
note the value to be inserted can be null
6.2 Arraylist.insertrange Method
inserts an element from the collection at the specified index of ArrayList
syntax: public virtual void Insertrange (int index, ICollection c);
Note the collection itself cannot be null. But it can include elements that are null
/*-----------------------------------------------------------------------------*/
ArrayList also has a lot of properties and methods that are not mentioned here and can be consulted on MSDN
Use of ArrayList classes in C #