Use of ArrayList in C

  • 2020-05-26 10:01:35
  • OfStack

The System.Collections.ArrayList class is a special array. You can dynamically change the length of an array by adding and removing elements.

1. advantages

1. Support for automatic resizing
2. You can insert elements flexibly
3. You have the flexibility to delete elements

2. limitations

It's a little bit slower than a 1-like array

3. Add elements

1. publicvirtualintAdd (objectvalue);
Add the object to the end of 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 the element at the specified index of ArrayList
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Insert(0,"aa");
The results for aaabcde
3. publicvirtualvoidInsertRange (intindex ICollectionc);
Inserts an element of the collection into the specified index of ArrayList
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 results for abtttttcde

4. delete

a)publicvirtualvoidRemove(objectobj);
Remove the first match of a particular object from ArrayList, notice the first
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Remove("a");
The results for bcde
2.publicvirtualvoidRemoveAt(intindex);
Removes the element at the specified index of ArrayList
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveAt(0);
The results for bcde
3. publicvirtualvoidRemoveRange (intindex intcount);
Remove 1 scoped element from ArrayList. Index represents the index, and count represents the number starting at the index
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveRange(1,3);
The results for ae
4. publicvirtualvoidClear ();
Remove all elements from ArrayList.
5. The sorting
a)publicvirtualvoidSort();
Sort the elements in ArrayList or part 1 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 results for eabcd
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList. Sort (); / / sorting
DropDownList1.DataSource=aList;//DropDownListDropDownList1;
DropDownList1.DataBind();
The results for abcde
b)publicvirtualvoidReverse();
Reverse the order of the elements in ArrayList or part 1 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 results for edcba

6. To find the

a)publicvirtualintIndexOf(object);
b)publicvirtualintIndexOf(object,int);
c)publicvirtualintIndexOf(object,int,int);
Returns a zero-based index of the first match of a value in ArrayList or part 1 of it. It didn't return negative 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 "); // I can't find it, minus 1
d)publicvirtualintLastIndexOf(object);
e)publicvirtualintLastIndexOf(object,int);
f)publicvirtualintLastIndexOf(object,int,int);
Returns a zero-based index of the last match of a value in ArrayList or part 1 of it.
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
a aList. Add (" "); / / to zero
aList.Add("d");
aList.Add("e");
a intnIndex = aList. LastIndexOf (" "); // is 2 instead of 0
g)publicvirtualboolContains(objectitem);
Determine whether an element is in ArrayList. Contains return true, otherwise return false

7. 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 ArrayList.
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. If Count exceeds Capacity when an element is added, the capacity of the list is doubled by automatically redistributing the internal array.
If the value of Capacity is explicitly set, the internal array also needs to be reallocated to hold 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 cut is the default capacity of 16, not 0
3. publicvirtualvoidTrimToSize ();
Set the capacity to the actual number of elements in ArrayList.
If no new elements are added 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 ArrayList sets the capacity of ArrayList to the default, 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;

Related articles: