The 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.
Speaking in Microsoft's words:
Any reference or value type added to ArrayList is implicitly cast upward to Object. If the item is a value type, it must be boxed when it is added to the list, and the unboxing operation is performed when it is retrieved. Both casting and boxing and unboxing operations degrade performance, and the effect of boxing and unboxing is noticeable in cases where large collections must be cycled. ”
ArrayList Inheritance diagram
650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/02/241ba3454fd6481b566595f9ff3827d3.png-wh_500x0-wm_3 -wmp_4-s_1309880916.png "title=" image 1.png "alt=" 241ba3454fd6481b566595f9ff3827d3.png-wh_ "/>
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
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 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
Alist.remove ("a");
The result is BCDE
2.public virtual void RemoveAt (Intindex);
removes the element at the specified index of the ArrayList
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.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.
Dropdownlist1.datasource=alist;//dropdown ListDropDownList1;
Dropdownlist1.databind ();
The result is EABCD
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.
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.
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.
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=capac
This article is from the "13233523" blog, please be sure to keep this source http://13243523.blog.51cto.com/13233523/1978436
How to use C # ArrayList