The System.Collections.ArrayList class is a special array. By adding and removing elements, you can dynamically change the length of the array.
First, the advantages
1. Support Automatic size change function
2. Flexibility to insert elements
3. Ability to remove elements flexibly
4. Flexible access to elements
II. Limitations
It's a little bit faster than a normal array.
Third, add 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
ArrayList alist=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,icollectionc);
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
Iv. deletion
A) public virtual void Remove (object obj);
Remove the first occurrence of a specific object from ArrayList, note that the first
ArrayList alist=new ArrayList ();
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 (Intindex);
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 virtual void Clear ();
Removes all elements from the ArrayList.
Five, sort
A) public virtual void Sort ();
Sorts the elements in a ArrayList or part of it.
Arraylistalist=newarraylist ();
Alist.add ("E");
Alist.add ("a");
Alist.add ("B");
Alist.add ("C");
Alist.add ("D");
Dropdownlist1.datasource=alist;//dropdown ListDropDownList1;
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");
Intnindex=alist.indexof ("a");//1
Nindex=alist.indexof ("P");//Not Found,-1
d) public virtual int LastIndexOf (object);
e) public virtual int LastIndexOf (object,int);
f) Public virtual int LastIndexOf (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");
Intnindex=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
Vii. getting the elements in an array
The following is an example of an integer that gives a way to get 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]+ "";//Gets the same way as the normal array, using the subscript method
Results: 0 1 2 3 4 5 6 7 8 9
Viii. Other
1. public virtual Intcapacity{get;set;}
Gets or sets the number of elements that ArrayList can contain.
2. public virtual Intcount{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;
This article transferred from: http://blog.163.com/fei_lai_feng/blog/static/928996220085269336629/
How to use C # ArrayList