This article mainly introduces the
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. Features that support automatic resizing 2. You can insert the element 3 flexibly. can be flexible to delete elements two. Limitations compared to general arrays, the speed is worse than three. add element 1. Publicvirtualintadd (Objectvalue); Adds an object to the end of the ArrayList Arraylistalist=newarraylist (); Alist.add ("a"); Alist.add ("B"); Alist.add ("C"); Alist.add ("D"); Alist.add ("E"); Content is ABCDE 2. Publicvirtualvoidinsert (Intindex,objectvalue); Inserts an element into the ArrayList at the specified index arraylistalist=newarraylist (); Alist.add ("a"); Alist.add ("B"); Alist.add ("C"); Alist.add ("D"); Alist.add ("E"); Alist.insert (0, "AA"); The result was AAABCDE 3. Publicvirtualvoidinsertrange (Intindex,icollectionc); Inserts an element from the collection into the ArrayList at the specified index arraylistalist=newarraylist (); Alist.add ("a"); Alist.add ("B"); Alist.add ("C"); Alist.add ("D"); Alist.add ("E"); Arraylistlist2=newarraylist (); List2. ADD ("tt"); List2. ADD ("TTT"); Alist.insertrange (2,LIST2); The result was ABTTTTTCDE four. Delete a) publicvirtualvoidremove (objectobj); Removes the first occurrence of a particular object from the ArrayList, noting that the first arraylistalist=newarraylist (); Alist.add ("A"); Alist.add ("B"); Alist.add ("C"); Alist.add ("D"); Alist.add ("E"); Alist.remove ("a"); The result is BCDE 2.publicvirtualvoidRemoveAt (intindex); Removes the element alist.add ("a") at the specified index of the ArrayList; Alist.add ("B"); Alist.add ("C"); Alist.add ("D"); Alist.add ("E"); Alist.removeat (0); The result was BCDE 3. Publicvirtualvoidremoverange (Intindex,intcount); Removes a certain range of elements from the ArrayList. Index represents the indexes, and count represents the number of Alist.add ("a") starting at the index; Alist.add ("B"); Alist.add ("C"); Alist.add ("D"); Alist.add ("E"); Alist.removerange (1,3); The result is AE 4. Publicvirtualvoidclear (); Removes all elements from the ArrayList. Five Sort a) publicvirtualvoidsort (); 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;//dropdownlistdropdownlist1; Dropdownlist1.databind (); The result was EABCD arraylistalist=newarraylist (); Alist.add ("a"); Alist.add ("B"); Alist.add ("C"); Alist.add ("D"); Alist.add ("E"); Alist.sort ();//Sort dropdownlist1.datasource=alist;//dropdownlistdropdownlist1; Dropdownlist1.databind (); Result isABCDE b) Publicvirtualvoidreverse (); Reverses the order of elements in a ArrayList or part of it. Arraylistalist=newarraylist (); Alist.add ("a"); Alist.add ("B"); Alist.add ("C"); Alist.add ("D"); Alist.add ("E"); Alist.reverse ()//reverse dropdownlist1.datasource=alist;//dropdownlistdropdownlist1; Dropdownlist1.databind (); The result was EDCBA six. Find a) Publicvirtualintindexof (object); b) Publicvirtualintindexof (object,int); c) Publicvirtualintindexof (Object,int,int); Returns the zero-based index of the first occurrence of a value in a ArrayList or part of it. Not found return-1. Arraylistalist=newarraylist (); 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 publicvirtualintlastindexof (object); e) Publicvirtualintlastindexof (object,int); f) Publicvirtualintlastindexof (Object,int,int); Returns the zero-based index of the last occurrence of a value in a ArrayList or part of it. Arraylistalist=newarraylist (); Alist.add ("a"); Alist.add ("B"); Alist.add ("a") and/or 0 alist.add ("D"); Alist.add ("E"); Intnindex=alist.lastindexof ("a");//value 2 instead of 0 g) PublicvirtualboolcontaIns (Objectitem); Determines whether an element is in ArrayList. Contains return true, otherwise false seven. Other 1. Publicvirtualintcapacity{get;set} Gets or sets the number of elements that ArrayList can contain. 2. Publicvirtualintcount{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 that are 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 doubles by automatically reallocating the internal array. If the value of the 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. After calling clear, Count is 0, and capacity is the default capacity of 16 instead of 0 3. Publicvirtualvoidtrimtosize (); Sets the capacity to the actual number of elements in the 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 calling TrimToSize. Truncating an empty ArrayList sets the capacity of the ArrayList to the default capacity, not zero. Arraylistalist=newarraylist (); Alist.add ("a"); Alist.add ("B"); Alist.add ("C"); Alist.add ("D"); Alist.add ("E");//count=5,capacity=16, Alist.trimtosize ();//count=capacity=5;