A detailed explanation of the differences between array and ArrayList in C #

Source: Internet
Author: User
This article mainly describes the C # in the array and ArrayList detailed and the relevant information, the need for friends can refer to the following

The details and differences of array and ArrayList in C #

One, the use of the Array

  Type[]  typename=new type[size];

Or

Type[]  typename=new type[]{};

Variables of type array must be instantiated at the same time (if the initialization is at least the size of the array)

Usually we int[],string[] ... In fact, it's declaring an array of arrays.

Such as:

string [] srt=new string[]{"A", "B"};     Int[] A=new int[2]; string [] srt=new string[3];

(1): Type data types are not missing, and must be unified, but not as int[] a=new array[];

(2): The size of the array is not missing, otherwise C # is considered to be an error because the array is a fixed-length memory;

(3): The right side is a bracket [] instead of ()

Note: Array arrays do not provide add,clear,addrange. method, instead of setting or getting the value directly

such as:a[0] = 0; a[1] = 1;

Two, the use of C # ArrayList arrays:

var arrayList = new ArrayList ();      Arraylist.add (1);      Arraylist.add (2);      Arraylist.add (50.0); Supported in. NET 4.0. Specifically why there is no research on       foreach (var array in arrayList)      {        Console.WriteLine (array);      }

Three, the conversion of ArrayList and array to each other

var arrayList = new list<int> ();      Arraylist.add (1);      Arraylist.add (2);      Arraylist.add (+);      Copy the values in the ArrayList array to the array      int[] array1=new Int[arraylist.count];      Arraylist.copyto (array1); Method one      int[] array2 = Arraylist.toarray ();//Method Two

Iv. [the difference between array and ArrayList]

#1. A variable of type array must be instantiated at the same time (at least the size of the array is initialized), and ArrayList can be declared only first.

Such as:

int[] array = new ARRAY[3]; or int[] Array = {n/a}; or ArrayList myList = new ArrayList ();

These are legal and directly use the int[] array;

#2. An array can store only isomorphic objects, and ArrayList can store heterogeneous objects.

Homogeneous objects are objects of the same type, and if an array declared as int[] can hold only shaping data, string[] can only hold character data, except for arrays declared object[].

and ArrayList can store any different type of data (because it is stored in the boxed object type, in fact ArrayList inside is the use of "object[" _ITEMS; Such a private field to encapsulate the object's)

How #3 are stored in a CLR managed pair

The array is always stored continuously, and the ArrayList is not necessarily contiguous.

#4 Initialization Size

An array object must be initialized with a specified size, and the created array size is fixed.

The size of the ArrayList can be specified dynamically, and its size can be specified at initialization or unspecified, meaning that the object's space can be arbitrarily increased.

#5 array cannot arbitrarily add and remove items, and ArrayList can insert and delete items at any location.

V. [Similarities of Array and ArrayList]

#1 have index, which means that you can get and modify any item directly through index.
#2 the objects they create are placed in the managed heap.
#3 are able to enumerate themselves (because they all implement the IEnumerable interface).

Vi. [Some features of ArrayList]

var arrayList = new list<int> (2); Console.WriteLine (arraylist.capacity);            int size = 2;      for (int i = 0; i < size; i++)      {        arraylist.add (i);      }         Console.WriteLine ("Compressed Capacity:" +arraylist.capacity);

When size is 2 o'clock, "current capacity" in the output results is 2,
When size is 3 or 4 o'clock, "current capacity" is 4,
When size is 5~8, "current capacity" is 8,
When size is 9~16, "current capacity" is 16,

The result of the experiment is that each time the actual number of objects in the ArrayList (Arraylist.count) exceeds its capacity threshold, the threshold is automatically doubled

ArrayList myList = new ArrayList (5);      for (int i = 0; i < 3; i++)      {        mylist.add (i);      }      Console.WriteLine ("Actual Capacity:" + mylist.capacity);      Mylist.trimtosize ();      Console.WriteLine ("Compressed Capacity:" + mylist.capacity);            Console.ReadLine ();

Output:

actual capacity:5compressed capacity:3 
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.