Why does list insertion fail when sufficient size is provided on construction?

前端 未结 8 841
北恋
北恋 2020-12-20 20:47

If we have the following variable declaration:

List list = new List(5);

Why does this:

list.insert(2, 3);
         


        
8条回答
  •  生来不讨喜
    2020-12-20 21:19

    That is because the integer you specify in the constructor is the amount that the List can hold. When items are added, the list is automatically increased. The resizing is avoided when you specify an initial capacity that matches the number of items that you want to add.

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

    See the remarks section in the documentation

提交回复
热议问题