Detailed explanation of the difference between Array and ArrayList in C#

黄舟
Release: 2017-03-28 11:44:20
Original
1791 people have browsed it

This article mainly introduces the relevant information about the detailed explanation and differences between Array and ArrayList in C#. Friends in need can refer to

C# Detailed explanation and difference between Array and ArrayList

##1. Usage of Array

  type[]  typename=new type[size];
Copy after login

or

 type[]  typename=new type[]{ };
Copy after login

Array type

variableIt must be instantiated at the same time as the declaration (if initialized, at least the size of the array must be initialized)

Usually we int[], string[]...In fact, we declare a

array array

Such as:

 string [] srt=new string[]{"a","b"};

     int[] a=new int[2]; string [] srt=new string[3];
Copy after login

(1):type

Data type cannot be missing; and must be unified, not such as int[] a =new Array[];

(2): The size of the array cannot be missing, otherwise C# will think it is an error because the array is a fixed-length memory;

(3): Right It is a square bracket [], not ()

Note: array array does not provide add, clear, addRange.. methods, but directly sets or gets the value

For example:

a[0] = 0; a[1] = 1;

2, Usage of C# ArrayList array:

var arrayList = new ArrayList();

      arrayList.Add(1);
      arrayList.Add(2);
      arrayList.Add(50.0); //在.net 4.0 支持。具体为什么还没有研究 
      foreach (var array in arrayList)
      {
        Console.WriteLine(array);
      }
Copy after login

3, The conversion between ArrayList and Array

 var arrayList = new List<int>();
      arrayList.Add(1);
      arrayList.Add(2);
      arrayList.Add(50);

      //ArrayList 数组中的值拷贝到Array中去
      int[] array1=new int[arrayList.Count];
      arrayList.CopyTo(array1); //方法一
      int[] array2 = arrayList.ToArray(); //方法二
Copy after login

4. [The difference between Array and ArrayList]

#1. Array type variables are declared at the same time It must be instantiated (at least the size of the array must be initialized), while ArrayList can just be declared first.


For example:

int[] array = new array[3];
 或 int[] array = {1,2,3};
 或 ArrayList myList = new ArrayList();
Copy after login

These are all legal, but using int[] array; directly is not possible.

#2. Array can only store homogeneous

objects, while ArrayList can store heterogeneous objects.

Isomorphic objects refer to objects of the same type. If an array is declared as int[], it can only store integer data, and string[] can only store

character data , except arrays declared as object[].

ArrayList can store any different types of data (because it stores boxed Object objects. In fact, ArrayList uses "object[] _items;" internally) Private fields to encapsulate objects)

#3 Storage method in CLR managed pair


Array is always stored continuously, while ArrayList is not necessarily stored continuously.

#4 Initialization size


The initialization of the Array object must only be of the specified size, and the size of the array after creation is fixed,

The size of ArrayList can be dynamically specified, and its size can be specified during initialization or not specified, which means that the space of the object can be increased arbitrarily.

#5 Array cannot add and delete items at will, while ArrayList can insert and delete items at any position.

5. [Similarities between Array and ArrayList]

#1 Both have indexes, that is, any item can be directly obtained and modified through index.

#2 The objects they create are placed in the managed heap.
#3 can all enumerate themselves (because they all implement the IEnumerable interface).

6. [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);
Copy after login

When size is 2, the "current capacity" in the output result is 2,

When size is When 3 or 4, "current capacity" is 4,
When size is 5~8, "current capacity" is 8,
When size is 9~16, "current capacity" is 16,

Through experiments, we can draw a conclusion that whenever the number of actual objects in ArrayList (ArrayList.Count) exceeds its own Capacity threshold, the threshold will automatically double

 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();
Copy after login

Output:

actual capacity:5
compressed capacity:3
Copy after login

The above is the detailed content of Detailed explanation of the difference between Array and ArrayList in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!