Why the insertion of the list is not performed when a sufficient size is provided for the construction?

If we have the following variable declaration:

List<int> list = new List(5); 

Why is this:

 list.insert(2, 3); 

It is issued with the following error:

 Index must be within the bounds of the List. 

What is the point of providing an initial size?

+6
source share
8 answers

All initial sizes give a hint of implementation in order to have at least a given capacity . It does not create a list populated with N entries by default; my attention:

Initializes a new instance of the List<T> class , which is empty and has the specified initial capacity.

If you continue to record MSDN in the Remarks section, you will find out why this constructor overload is provided (again, my emphasis):

The capacity of a List<T> is the number of elements that List<T> can hold. Since the elements are added to the List<T> , the capacity automatically increases in accordance with the need to redistribute the internal array.

If the size of the collection can be estimated, specifying the initial capacity eliminates the need for several resizing operations when adding items to the List<T> .

In short, List<T>.Count does not match List<T>.Capacity ("If the number exceeds the Capacity when adding items, the capacity increases ...").

You get an exception, because the list only logically contains the elements that you add, changing the capacity does not change the number of logically stored elements. If you were to set List<T>.Capacity less than List<T>.Count , we can test this behavior in the other direction:

 Unhandled Exception: System.ArgumentOutOfRangeException: capacity was less than the current size. Parameter name: value at System.Collections.Generic.List`1.set_Capacity(Int32 value) 

To create the behavior you are looking for:

 public static List<T> CreateDefaultList<T>(int entries) { return new List<T>(new T[entries]); } 
+8
source

The size in the constructor indicates how much is allocated for the background array - it is nevertheless, however, empty (simple: empty with a certain amount of initial space).

You can include it in the used part of the list or at the end.

+2
source

Internally, a List (T) is implemented using an array in the background. When you initialize the list this way, you simply set the size of the underlying array, which changes as the list grows. Thus, you initialize the initial capacity. This does not mean that there are many elements in your list.

You add items to the list by first initializing it, and then adding items to it using .Add(item) .

+2
source

Since the insertion assumes that there are actually a lot of elements already inserted in the list - capacity is not the same as size. Initializing a list with a given capacity simply sets the size of the internal array - this is an optimization to prevent the array from being resized when you know the number of elements you are going to insert.

0
source

The List (int) constructor indicates the initial capacity of the list. It does not indicate the number of source elements. When building, the list is empty, so any insertion can be performed only with index 0.

0
source

The initial size is used to indicate the size of the internal array.

When you insert elements into a list, it stores them in an array. When the array is full, it creates a new double-sized array and copies all the elements. If you have an idea that you are going to place 5000 elements, you will need to specify this hint so that it does not do a lot of resizing / copying arrays.

The original size does not indicate that there are any items in the list.

0
source

This is because the integer specified in the constructor is the amount that List can hold. When items are added, the list automatically grows. Resizing can be avoided by specifying an initial capacity that matches the number of items you want to add.

However, you still have to use the Add method to add new elements.

See the notes section in the documentation.

0
source

Use

 listItem.Addrange(number); 
0
source

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


All Articles