In C #, if you want the length of an array and the number of elements to change as the program runs, you can use the ArrayList class, which is an array that dynamically increases or decreases the members.
The common properties and methods of ArrayList class
1. Common properties of the ArrayList class
2. Common methods of the ArrayList class
Two, the difference between the ArrayList class and the array class
? The ArrayList class is actually an optimized version of the array class.
? ArrayList can define only one-dimensional arrays, and arrays may define multidimensional arrays.
? The lower bound of the ArrayList is always 0,array to define its own lower limit.
? ArrayList elements are of type object, so boxing and unboxing are required, and memory allocations are expensive, and the elements of an array are usually of a specific type.
? The number of elements in the ArrayList can be expanded automatically, and the number of elements in the array is fixed.
? ArrayList has methods such as add, delete, insert, move, and copy.
Iii. examples
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Collections; Import a ArrayList namespace
Namespace Test
{
Class Program
{
static void Main (string[] args)
{
C # dynamic Array arraylist-www.baike369.com
ArrayList alist = new ArrayList ();
Alist. ADD ("one"); adding objects
Alist. ADD ("both");
Console.Write ("element added to the array:");
foreach (Object obj in alist)
{Console.Write (obj);}
Console.WriteLine ();
Console.WriteLine ("The number of array elements actually contained:" + alist. Count);
Console.WriteLine ("The maximum number of array elements that can currently be included:" +alist. capacity);
Alist. Insert (1, "C #"); Inserts an element in the specified index position C #
Console.Write ("Insert at index value 1:");
foreach (Object obj in alist)
{Console.Write (obj);}
Console.WriteLine ();
Console.WriteLine ("The number of array elements actually contained:" + alist. Count);
Console.WriteLine ("The maximum number of array elements that can currently be included:" +alist. capacity);
Alist. Add (". ");
Console.WriteLine ("Add. ");
Console.WriteLine ("The maximum number of array elements that can currently be included:" +alist. capacity);
Alist. ADD ("---"); A change in the number of bits occurs when the aggregate capacity is insufficient
Console.WriteLine ("Add---");
Console.WriteLine ("The number of array elements actually contained:" + alist. Count);
Console.WriteLine ("The maximum number of array elements that can currently be included:" +alist. capacity);
To get the value of a collection element in an indexed manner
Console.WriteLine ("No. 3rd Index:" + alist[3);
Use the Contains method to find if the collection contains "?"
Console.WriteLine ("Does the array contain?:" + alist. Contains ("?"));
Console.Write ("data element after previous operation:");
foreach (Object obj in alist)
{Console.Write (obj);}
Console.WriteLine ();
Console.WriteLine ("The number of array elements actually contained:" + alist. Count);
Console.WriteLine ("The maximum number of array elements that can currently be included:" +alist. capacity);
Alist. Remove (". "); Removes "in the collection. The element
Alist. Remove ("?"); Remove the "?" From the collection Elements
Console.WriteLine ("Not included, reduced by 1 elements, capacity unchanged");
foreach (Object obj in alist)
{Console.Write (obj);}
Console.WriteLine ();
Console.WriteLine ("The number of array elements actually contained:" + alist. Count);
Console.WriteLine ("The maximum number of array elements that can currently be included:" +alist. capacity);
Alist. RemoveAt (3); Remove elements from index 3rd bits
Console.Write ("remove element of index 3rd bit:");
foreach (Object obj in alist)
{Console.Write (obj);}
Console.WriteLine ();
Console.WriteLine ("The number of array elements actually contained:" + alist. Count);
Console.WriteLine ("The maximum number of array elements that can currently be included:" +alist. capacity);
Alist. TrimToSize (); Reduced capacity
Console.WriteLine ("The maximum number of array elements that can currently be included:" +alist. capacity);
Alist. Clear (); Clears all elements in the ArrayList
Console.WriteLine ("After clearing all elements in the ArrayList:");
Console.WriteLine ("The number of array elements actually contained:" + alist. Count);
Console.WriteLine ("The maximum number of array elements that can currently be included:" +alist. capacity);
Alist. TrimToSize (); Reduce capacity again
Console.WriteLine ("The maximum number of array elements that can be included after the capacity is reduced again:"
+alist. capacity);
Console.ReadLine ();
}
}
}
Operation Result:
C # dynamic Array ArrayList