Does Arraylist use Array?

Is ArrayList an internal use of an array? Is this an empty array if we use the default constructor (new ArrayList ())? Thanks.

+4
source share
3 answers

Yes Yes. One of the easiest ways to verify this is to look at the source. You can either get the reference source or just use the .NET Reflector to decompile the .NET DLL.

Here's the relevant part of the ArrayList, from Reflector:

public class ArrayList : IList, ICollection, IEnumerable, ICloneable { static ArrayList() { emptyArray = new object[0]; } public ArrayList() { this._items = emptyArray; } private object[] _items; private static readonly object[] emptyArray; // etc... } 

You should not always rely on this. This is an implementation detail and may change in future versions of .NET (although this is probably not the case). Also for new code you should use List<T> instead of ArrayList .

+7
source

Yes, ArrayList uses an array to store elements.

If you create an ArrayList without specifying capacity, the default initial capacity is used. The fact that the initial capacity by default may depend on the version of the framework. For framework 2, it is zero. In framework 1, I think it was 16.

+3
source

Yes .. ArrayList uses an array by itself.

It contains an array of objects (in java. C # should also be the same) to give you uniformity. Although the arraylist seems to be very dynamic in allocating memory, its internal operations are full of arrays.

The time when you create an object by calling the constructor, internally it calls an array with a limited size, maybe 10 (actually exactly 10 in java). Then, when and when you add objects to the arraylist, it will also need to increase the internal array. Therefore, the size of the internal array must be increased. Thus, a new array is created with a double size, and the old values ​​are copied to this new array. Please note that the capacity of the array is increasing, so you can add more objects.

+2
source

Source: https://habr.com/ru/post/1307898/


All Articles