A ArrayList related methods of learning
Using enumerator to access arrays:
ArrayList list = new ArrayList (1);
for (int i = 0; i < i++)
List. ADD (i);
IEnumerator ETR = list. GetEnumerator ();//Enumeration
while (ETR. MoveNext ())
Console.Write (ETR. Current + "");
Run Result:
By the right, if you want to use ArrayList in C #, you must include a reference in the System namespace, or there will be an error. Using System.Collections;
Here are some common methods for dynamic arrays:
ArrayList list = new ArrayList (5);
Construct a dynamic array
List. Clear ()//clearly all elements in the array
Console.WriteLine ();
Console.WriteLine ("There are {0} elements in the list", list. Count);
ArrayList Shuzu = new ArrayList (5);
Shuzu. ADD ("Paladin One");
Shuzu. Add ("Liang Jian");
Shuzu. ADD ("Bianceng.");
Shuzu. ADD ("National Treasures");
Shuzu. ADD ("Lu Xiaofeng");
for (int x = 0; x <= shuzu. Count-1; X + +)
Console.WriteLine (Shuzu[x]);
Console.WriteLine ("Now reverse it\n");
Shuzu. Reverse ();
Console.WriteLine ("After reverser\n");
Console.WriteLine ();
for (int x = 0; x <= shuzu. Count-1; X + +)
Console.WriteLine (Shuzu[x]);
Run Result:
Now compare the next Array and Arraylist:array to provide some flexibility that ArrayList does not have. For example:
You can set the lower limit of an Array, but the lower bound of the ArrayList is always zero.
Array can have more than one dimension, and ArrayList is always one-dimensional; a specific type (excluding Object) array has better performance than ArrayList because the ArrayList element is of type Object, so the value type is often stored or retrieved Raw boxing and unboxing.
Array is in the System namespace; ArrayList is located in System. In the Collections namespace
As for the ArrayList of the parent class inherited from the object class, I'm not going to introduce you here,
That's the basic method. Let's look at some of the methods used by ArrayList.
|
Console.WriteLine ();
for (int x = 0; x <= shuzu. Count-1; X + +)
Console.WriteLine (Shuzu[x]);
Console.WriteLine ("There are {0} elements in the list", Shuzu. Count);
Console.WriteLine ("Now add a new elements");
Shuzu. Add ("new element");//Add an element
Console.WriteLine ("There are {0} elements in the list", Shuzu. Count);
Shuzu. Remove ("Liang Jian");//delete Feature element
Console.WriteLine ("There are {0} elements in the list", Shuzu. Count);
Shuzu. RemoveAt (2);//delete element at specified location
Console.WriteLine ("There are {0} elements in the list", Shuzu. Count);
Shuzu. Insert (1, "system level");//Inserts an element at the location where the array is made
Console.WriteLine ("There are {0} elements in the list", Shuzu. Count);
int countelement = Shuzu. Count;
Console.WriteLine (Shuzu. GetType ());//type of output array
int index = Shuzu. IndexOf ("system level");//Get the index number of an element
Console.WriteLine ();
ArrayList anothe = new ArrayList (Shuzu);//Create an array
for (int b = 0; b < anothe. Count; b++)
Console.WriteLine (Anothe[b]);
Console.WriteLine (Shuzu. Contains ("National Treasure"))//Determine whether an element exists in the array
Console.WriteLine (Shuzu. LastIndexOf ("National Treasure"));
|
List. ADD ("B");
List. ADD ("G");
List. ADD ("J");
List. ADD ("S");
List. ADD ("M");
string[] array1 = new string[list. Count];
Create a string array of the same size as the I and Shuzu elements
List. CopyTo (array1, 0);
Copy elements from the first element in Shuzu to the array
object[] Array2 = list. ToArray ()//Use this method to create an array
Console.WriteLine ("Array 1:");
foreach (string s in Array2)
{
Console.Write ("{0}", s);
}
Console.WriteLine ();
foreach (string s in array1)
{
Console.Write ("{0}", s);
}
Console.WriteLine ();
for (int i = 0; i < list. Count; i++)
Console.Write (List[i]); |
|
The results of the above program run are:
|
|