From: A brief talk on ArrayList internal principle
System.Collections.ArrayList is what we often call dynamic arrays, and is one of the "data types" that we commonly use.
This is stated on MSDN: The IList interface is implemented using an array of sizes that can be dynamically added on demand.
Let me explain: a non-generic collection of objects that can dynamically increase the size and can be accessed individually by index as needed.
The average person thinks that ArrayList is a "pure dynamic" array, similar to the principle of < data structure > "Linked list".
I thought so before I did a thorough research and study on ArrayList. But that is not the case.
There is an array of type Object inside ArrayList, and when the element is first added, it is initialized and the capacity is set to 4.
If the current array has the remaining space, then we can add the element directly to the appropriate location.
Otherwise, when the array space is exhausted, the. NET runtime creates a new array that is twice times the size of the original array and copies the elements from the original array to the new array.
Finally, assign the address of the new array to the variable of the old array.
Let's take a look at the following code:
1 Public classArrayList2 {3 /// <summary>4 ///Default Capacity5 /// </summary>6 Private Const intDefaultcapacity =4;7 8 /// <summary>9 ///the number of elements in the current arrayTen /// </summary> One Private intsize; A - /// <summary> - ///an array to hold the elements the /// </summary> - Private Object[] items; - - /// <summary> + ///Static read-only Object type array for initializing items - /// </summary> + Private Static ReadOnly Object[] emptyarray; A at /// <summary> - ///the current maximum capacity - /// </summary> - Public intcapacity - { - Get in { - //returns the length of the current array to return This. Items. Length; + } - Set the { * //The value set is not equal to the length of the current items array $ if(Value! = This. Items. Length)Panax Notoginseng { - if(Value >0) the { + //declares a new array of type Object with the value set to the length A Object[] Destinationarray =New Object[value]; the + if( This. Size >0) - { $ //and copies the elements in the items array into the Destinationarray array $Array.copy ( This. Items,0, Destinationarray,0, This. Size); - } - the //and assign Destinationarray to the items array - This. Items =Destinationarray;Wuyi } the Else - { Wu This. Items =New Object[4]; - } About } $ - } - } - A /// <summary> + ///Static Constructors the ///initializes the Emptyarray to an array of type Object with a capacity of 0. - /// </summary> $ StaticArrayList () the { theEmptyarray =New Object[0]; the } the - /// <summary> in ///no parameter constructor the ///assigns the value of the Emptyarray array to the items array; the ///that is, the items are initialized with an array of type Object of size 0. About /// </summary> the PublicArrayList () the { the This. Items =Emptyarray; + } - the /// <summary>Bayi ///ways to add elements the /// </summary> the /// <param name= "value" >objects passed in</param> - /// <returns>Index of the currently added element</returns> - Public intADD (Objectvalue) the { the //if the number of elements in the current array equals the length of the items array the if( This. Size = = This. Items. Length) the { -Ensurecapacity ( This. Size +1); the } the //add incoming elements to the items array the This. items[ This. Size] =value;94 //returns the index of the current element in the items array the return This. size++; the } the 98 /// <summary> About ///ensure capacity is met - /// </summary>101 /// <param name= "min" ></param>102 Private voidEnsurecapacity (intmin)103 {104 if( This. Items. Length <min) the {106 //if the length of the current array of elements is equal to 0, num equals 4, otherwise num equals twice times the length of the current element array .107 intnum = ( This. Items. Length = =0) ?4: ( This. Items. Length *2);108 if(Num <min)109 { thenum =min;111 } the //sets the current maximum capacity to Num113 This. Capacity =num; the } the } the}
This code is a small part of what I have extracted from the internal code of the ArrayList class, with detailed comments, and I won't say much.
[Turn] on the internal principle of ArrayList