I. Example of C # array usage:
Type [] typename = new type [size];For example, int [] a = new int [2]; string [] str = new string [5];
In fact, we usually use int [], string [].... At this time, we have created an Array, but we do not have this awareness.
(1): The type data type must not be missing, and must be unified, rather than int [] a = new Array [];
(2): The size and size of the array cannot be missing; otherwise, c # considers it an error because the array is a fixed-length memory;
(3): The right side is a braces [], instead ()
Instance:
// Array ar = new Array (); // error. You cannot create an abstract class or an instance with the interface "System. Array"
// Int [] array = new Array [3]; error,
// Int [] array = new Array []; error. The array size or initial value must be specified.
// Int [] array = new Array [3]; // error. cannot be converted to int []
// Int [] array = new int []; // error. The array size or initial value must be specified.
// We usually use int [], string []... to declare an array.
Int [] array = new int [5];
// Or int [] arr = {1, 2, 3 };
For (int I = 0; I <5; I ++)
{
Array [I] = I * 100; // note: the array does not provide the add, clear, addRange... method, but directly sets or obtains the value.
Response. Write (array [I] + "<br> ");
}
Response. Write (array. Length + "<br> ");
Or
Int [] intarray = new int [9];
Intarray [0] = 10;
Intarray [1] = 20;
Intarray [2] = 30;
Intarray [3] = 40;
Intarray [4] = 50;
Intarray [5] = 60;
Intarray [6] = 70;
Intarray [7] = 80;
Intarray [8] = 90;
Ii. Example of C # ArrayList array usage:
ArrayList al = new ArrayList ();
For (int I = 0; I <3; I ++)
{
Al. Add (I );
Response. Write (al [I]. ToString () + "<br>"); // output the element value in the array
// Response. Write (al [I] + "<br> ");
}
Response. Write (al. Count + "<br> ");
Foreach (int obj in al) // or foreach (object obj in al), because al is an object-type array
{
Response. Write (obj + "-OK" + "<br> ");
}
3. Conversion between ArrayList and Array
(1) The following is an example of copying the values in the ArrayList Array to the Array.
// Int [] IResult = new int [al. Count];
// Al. CopyTo (IResult );
// Or use the following method
Int [] IResult = (int []) al. ToArray (typeof (Int32); // ToArray (Int32); this error must be forcibly converted.
// Person [] personArrayFromList = (Person []) personList. ToArray (typeof (Person ));
Foreach (int item in IResult)
{
Response. Write (item. ToString ());
}
Response. Write ("The following is an example of copying the values in the Array to ArrayList" + "<br>" + ": <br> ");
Int [] a ={ 222,333,555 };
ArrayList arrList = new ArrayList ();
Foreach (object obj in a) // or foreach (int obj in)
{
ArrList. Add (obj );
Response. Write (obj + "<br> ");
}