C # differences between array and arraylist and list
ASP. NET learning read 3169 comments 1
Font size: large, medium, small
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 ()
Technorati label: arraylist
Instance:
// array AR = new array (); // error. The abstract class or interface "system. array instance
// 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 []... actually, an array is declared.
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 ++)
{< br> Al. add (I);
response. write (Al [I]. tostring () + "
"); // output the element value in the array
// response. write (Al [I] + "
");
}< br> response. write (Al. count + "
");
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> ");
}