Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "Index was out of range" exception for List<T> but not for arrays?

When I initialize an array and access elements using the indexer, that works just fine:

object[] temp = new object[5];
temp[0] = "bar";

Now I would expect the same to work for a List<T>, given you can initialize it by passing the capacity to the constructor:

List<object> temp = new List<object>(5);
temp[0] = "bar";

This last line however throws the following exception:

Index was out of range. Must be non-negative and less than the size of the collection

Why does this happen for the List<T> type, but not for the array? Since arrays are just lower level abstractions for collections for the CLR, then why does this exception occur?


Original question by Awais Mahmood.

like image 983
O. R. Mapper Avatar asked Jan 28 '16 12:01

O. R. Mapper


People also ask

How do you fix array index out of range?

Your counter should be the position in the array and not the value of that position. Change counter < fiblist[4] to counter < 4 and change int counter = fiblist[14] to int counter = 14 to fix the problem.

How do you solve system IndexOutOfRangeException index was outside the bounds of the array?

Solutions to Prevent IndexOutOfRangeException Solution 1: Get the total number of elements in a collection and then check the upper bound of a collection is one less than its number of elements. Solution 2: Use the try catch blocks to catche the IndexOutOfRangeException .

What is array index out of range?

The ArrayIndexOutOfBoundsException is one of the most common errors in Java. It occurs when a program attempts to access an invalid index in an array i.e. an index that is less than 0, or equal to or greater than the length of the array.

What does index was out of range mean?

Generally, list index out of range means means that you are providing an index for which a list element does not exist.


1 Answers

Short answer: Because the 5 does very different things.

Long answer:

When initializing an array, you set its size, and that size is fixed. The array cannot grow or shrink later on. Therefore,

object[] temp = new object[5];

means that you create a new array with 5 elements. Hence, you can access these elements right after creating the array.

For lists, size is variable. Instances of the List<T> class internally use an array for storing their items, and when you add or remove items in the list, that array gets replaced with a bigger or smaller array. On each of these occasions, all items that remain in the list get copied from the previous array to the new one. As this is quite a costly operation, the internal array has some overhead of unused items. As long as items are added to the list and the size of that internal array is not exceeded, the array does not need to be replaced.

The 5 that you pass to the constructor of the list is the initial size of that internal array:

List<object> temp = new List<object>(5);

That means, the list you create has zero elements (hence the exception), but the internal array is initialized to a size of 5, so you can add 5 elements without requiring the internal array to be replaced.

like image 139
O. R. Mapper Avatar answered Sep 24 '22 08:09

O. R. Mapper