Detailed Explanation and Difference of Array and ArrayList in C

  • 2021-11-29 08:15:15
  • OfStack

Detailed Explanation and Difference of Array and ArrayList in C #

1. Usage of Array


  type[]  typename=new type[size]; 

Or


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

Variables of type Array must be instantiated at the same time as they are declared (if initialized, at least the size of the array must be initialized)

Usually we int [], string []... are actually declaring an array array

Such as:


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

     int[] a=new int[2]; string [] srt=new string[3];

(1): type data type is indispensable; And it should be unified as 1, but not as int [] a=new Array [];

(2): The size of the array size can not be missing, otherwise c # is considered to be wrong, because the array is a fixed length of memory;

(3): To the right is a bracket [], not ()

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

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); // In .net 4.0  Support. Why hasn't it been studied yet  
      foreach (var array in arrayList)
      {
        Console.WriteLine(array);
      }

3. Transformation between ArrayList and Array


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

      //ArrayList  The values in the array are copied to the Array Zhongqu 
      int[] array1=new int[arrayList.Count];
      arrayList.CopyTo(array1); // Method 1
      int[] array2 = arrayList.ToArray(); // Method 2

4. [Difference between Array and ArrayList]

# 1. Variables of type Array must be instantiated at the same time as they are declared (at least to initialize the size of the array), whereas ArrayList can just be declared first.

Such as:


int[] array = new array[3];
  Or  int[] array = {1,2,3};
  Or  ArrayList myList = new ArrayList();

These are all legal, while using int [] array directly; It won't work.

# 2. Array can only store homogeneous objects, while ArrayList can store heterogeneous objects.

Isomorphic objects refer to objects of the same type. Arrays declared as int [] can only store shaped data, while string [] can only store character data, except arrays declared as object [].

And ArrayList can store any different type of data (because it stores all boxed Object objects, in fact, ArrayList uses a private field like "object [] _ items;" to encapsulate objects)

How # 3 is stored in an CLR managed pair

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

# 4 initialization size

Array objects must be initialized to the specified size only, and the array size after creation is fixed,

However, the size of ArrayList can be dynamically specified, and its size can be specified at initialization or not, that is to say, the space of this object can be increased arbitrarily.

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

5. [Similarities between Array and ArrayList]

# 1 has an index (index), which means that any item can be fetched and modified directly through index.
# 2 All the objects they create are placed in the managed heap.
# 3 is able to enumerate itself (because both implement the IEnumerable interface).

6. [Some Characteristics 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, "current capacity" in the output result is 2.
"current capacity" is 4 when size is 3 or 4,
When size is 5 ~ 8, "current capacity" is 8,
When size is 9 ~ 16, "current capacity" is 16,

The experiment results show that whenever the actual number of 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();

Output:


actual capacity:5
compressed capacity:3

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: