Usage and conversion of Array and ArrayList in C #

Source: Internet
Author: User
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> ");
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.