In C #, how does List <T> store elements,
Jeffrey Zhao said, "How many elements do you have in your dictionary? "The article mentioned a question he asked during the interview: How does List store elements? Unfortunately, I can't answer the question myself. I only know how to use it, but I don't know why it is used. It's obvious that I don't know why ". As a result, the source code of a List <T> is used to get a glimpse.
Using System; using System. diagnostic; using System. collections. objectModel; using System. security. permissions; namespace System. collections. generic {... [Serializable ()] public class List <t>: IList <t>, System. collections. IList {private const int _ defaultCapacity = 4; private T [] _ items; // List <T> internal private int _ size that stores data by array _ items; // array length: private int _ version; [NoSerialized] private Object _ syncRo Ot; static T [] _ emptyArray = new T [0]; // No-parameter constructor sets _ items as an empty array public List () {_ items = _ emptyArray;} // This constructor returns an initial capacity public List (int capacity) to the _ items Array ){... items = new T [capaicty];} // This constructor copies the set type parameters to the _ items array public List (IEnumerable <t> collection ){... ICollection <t> c = collection as ICollection <t>; if (c! = Null) {int count = c. count; // assign the length of the constructor set type parameter to the temporary variable count _ items = new T [count]; // List <T> the length of the Internally maintained _ items array is the same as that of the constructor set type parameter c. copyTo (_ items, 0); // copy all the elements in the constructor set to the _ items array and delete _ size = count; // _ the length of the items array is the length of the constructor set type parameter} else {_ size = 0; _ items = new T [_ defacapcapacity];...} // set this attribute to change the length of the _ items array internally maintained by the List <t> public int Capacity {get {return _ items. length;} set {if (value! = _ Items. length) {// if the Length of the array _ items maintained by the current value assignment is inconsistent with that maintained by the List <t>, if (value <_ size) {// TODO: exception Handling} if (value> 0) {T [] newItems = new T [value]; // create a temporary and new array, if (_ size> 0) {// copy a temporary and new array to the List <t> internally maintained array _ items. Note, in this case, the length of _ items is a new value Array. copy (_ items, 0, newItems, 0, _ size) ;}} else {_ items = _ emptyArray ;}}} public void Add (T item) {if (_ size = _ items. length) EnsureCapacity (_ size + 1); _ items [_ size + +] = Item ;...} // make sure that the length of the _ items array internally maintained by List <t> is at least the given value. // if the original length of the _ items array is smaller than the given value, set the length of the _ items array to 2 times the original length privat void EnsureCapacity (int min) {if (_ items. length <min) {int newCapacity = _ items. length = 0? _ Defacapcapacity: _ items. Legnth * 2; if (newCapacity <min) newCapacity = min; Capacity = newCapacity ;}}}}
It can be seen that the general process of storing elements in the List <T> is as follows:
→ List <T> An array _ items is maintained internally to store T-type elements.
→ When a new T-type element is stored, the Add (T item) method is called.
→ The Add (T item) method calls the EnsureCapacity (int min) method internally to ensure that the value of the Capaicty attribute of the List <T> is at least 1 on the original length, up to twice the original length.
→ The length of _ items is expanded during the Capacity value assignment.
→ After resizing, store the new T-type elements.
Simply put:
When a new element is stored in List <T>, List <T> first expands the internal array maintained by it, and then adds the new element.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.