The System.Collections.ArrayList class is a special array. By adding and removing elements, you can dynamically change the length of the array.
A Advantages
1. Supports auto-size-changing features
2. Flexibility to insert elements
3. The ability to remove elements flexibly
Two Limitations
It's a little bit faster than a normal array.
Three adding elements
1. public virtual int Add (object value);
Add an object to the end of the ArrayList
ArrayList alist=new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Content is ABCDE
2. public virtual void Insert (int index,object value);
Inserts an element at the specified index of the ArrayList
Array listalist=new ArrayList ();
Alist.add ("a");
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,icollection c);
Inserts an element from the collection at the specified index of ArrayList
ArrayList alist=new ArrayList ();
Alist.add ("a");
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
Four Delete
A) public virtual void Remove (object obj);
Remove the first occurrence of a specific object from ArrayList, note that the first
ArrayList alist=newarraylist ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.remove ("a");
The result is BCDE
2.public virtual void RemoveAt (int index);
Removes the element at the specified index of the ArrayList
Alist.add ("a");
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);
Removes a range of elements from the ArrayList. Index, which represents the number that starts at the index
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.removerange (1,3);
Results for AE
4. Public virtualvoid Clear ();
Removes all elements from the ArrayList.
Five Sort
A) public virtual void Sort ();
Sorts the elements in a ArrayList or part of it.
ArrayList alist=new ArrayList ();
Alist.add ("E");
Alist.add ("a");
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 ("a");
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 the elements in the ArrayList or part of it.
ArrayList alist=new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
Alist.reverse ();//Invert
Dropdownlist1.datasource=alist;//dropdownlistdropdownlist1;
Dropdownlist1.databind ();
The result is EDCBA
Six Find
A) public virtual int IndexOf (object);
b) public virtual int IndexOf (object,int);
c) public virtual int IndexOf (object,int,int);
Returns the zero-based index of the first occurrence of a value in ArrayList or part of it. No return-1 found.
ArrayList alist=new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");
int Nindex=alist.indexof ("a");//1
Nindex=alist.indexof ("P");//Not Found,-1
d) Public virtual Intlastindexof (object);
e) public virtual intlastindexof (object,int);
f) Public virtual intlastindexof (object,int,int);
Returns the zero-based index of the last occurrence of a value in the ArrayList or part of it.
ArrayList alist=new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("a");//With 0
Alist.add ("D");
Alist.add ("E");
int Nindex=alist.lastindexof ("a");//value is 2 instead of 0
g) Public virtual bool Contains (Objectitem);
Determines whether an element is in ArrayList. Contains return true, otherwise returns false
Seven Other
1. public virtual int Capacity{get;set;}
Gets or sets the number of elements that ArrayList can contain.
2. public virtual int count{get;}
Gets the number of elements actually contained in the ArrayList.
Capacity is the number of elements that ArrayList can store. Count is the number of elements actually contained in ArrayList. Capacity is always greater than or equal to count. If count exceeds capacity when the element is added, the capacity of the list is doubled by automatically reallocating the internal array.
If the value of capacity 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.
When clear is called, Count is 0, and at this point capacity is the default capacity of 16 instead of 0
3. public virtual void trimtosize ();
Sets the capacity to the actual number of elements in ArrayList.
If you do not add new elements to the list, this method can be used to minimize the memory overhead of the list.
To completely clear all the elements in the list, call the Clear method before you call TrimToSize. Truncating the empty ArrayList will set the ArrayList capacity to the default capacity, not zero.
ArrayList alist=new ArrayList ();
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Alist.add ("E");//count=5,capacity=16,
Alist.trimtosize ();//count=capacity=5;
Using system;using System.collections;public class samplesarraylist {public static void Main () { // Creates and initializes a new ArrayList. ArrayList Myal = new ArrayList (); Myal.add ("Hello"); Myal.add ("World"); Myal.add ("!"); Displays the properties and values of the ArrayList. Console.WriteLine ("Myal"); Console.WriteLine (" Count: {0}", Myal.count); Console.WriteLine (" Capacity: {0}", myal.capacity); Console.Write (" Values:"); Printvalues (Myal); } public static void Printvalues (IEnumerable myList) { foreach (Object obj in myList) Console.Write ("
{0} ", obj); Console.WriteLine (); }} /* This code produces output similar to the Following:myal Count: 3 capacity:4 Values: hello< C31/>world !*/
ArrayList usage Notes