1

I realized that if my list is empty, I cannot insert something at 3rd index as it will throw an out of bounds error.

List<int> list = new List<int>();
list.Insert(3, 1000); //System.ArgumentOutOfRangeException

I will have to pad the first and the second elements with null for me to able to insert something at the 3rd index. This seems unnecessary. But if it was an array, I can insert something to any index I want even if the previous indices are not assigned.

int[] arr = new int[100];
arr[3] = 1000; //no error

Is there any way that I can insert to an empty list at a specific index without padding the previous index? Or is there any other better collection?

Julian
  • 33,915
  • 22
  • 119
  • 174
kenndawg
  • 29
  • 5
  • 3
    The elements of an array of `int` are always defined - they are automatically `0`. – Enigmativity Feb 13 '20 at 02:58
  • 2
    The equivalent of `List list = new List(); list.Insert(3, 1000);` is `int[] arr = new int[0]; arr[3] = 1000;`. Oddly enough it throws a different error. – Enigmativity Feb 13 '20 at 03:02
  • 1
    To put it another way, calling `new List()` defines an empty list, but calling `new int[100]` creates an array of `100` elements - so calling `new int[0]` creates an empty array. – Enigmativity Feb 13 '20 at 03:05
  • Another perfect example of lets read the documentation https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.8 – TheGeneral Feb 13 '20 at 03:06
  • Possibly helpful https://stackoverflow.com/questions/2430884/c-sharp-array-vs-generic-list – TheGeneral Feb 13 '20 at 03:10
  • 1
    @MichaelRandall - That's not a duplicate. It might help the OP move forward but it doesn't answer his specific questions. – Enigmativity Feb 13 '20 at 03:13
  • 2
    @Enigmativity which is why there was no dup hammer, though you are right *possible duplicate* could have been worded as *possibly helpful* – TheGeneral Feb 13 '20 at 03:13

2 Answers2

6

Other collection type

Or is there any other better collection?

You could use a Dictionary. Usage example:

using System.Collections.Generic;

...

var items = new Dictionary<int, int>();
items.Add(3, 1000); // will throw error if 3 already exists

// Or update 
items[3] = 1000: // will not throw error if already exists or doesn't exist!

Please note that the order of the dictionary is not guaranteed. From the docs

The order in which the items are returned is undefined.

If you need a guarantee of the ordering, you could use a SortedList or a SortedDictionary.

using System.Collections.Generic;

...

var items = new SortedList<int, int>();
items.Add(3, 1000); // will throw error if 3 already exists

// Or update 
items[3] = 1000: // will not throw error if already exists or doesn't exist!
using System.Collections.Generic;

...

var items = new SortedDictionary<int, int>();
items.Add(3, 1000); // will throw error if 3 already exists

// Or update 
items[3] = 1000: // will not throw error if already exists or doesn't exist!

The difference between SortedList and SortedDictionary is performance. Quote from the docs:

Where the two classes differ is in memory use and speed of insertion and removal

The (Sorted)Dictionary and SortedList works a bit different compared to lists and arrays. I would recommend to read the linked Microsoft documentation when using one of them.

null values in a list or array

I will have to pad the first and the second elements with null for me to able to insert something at the 3rd index

Please note that you cannot add null values in a list or array of ints. That would be only possible with "nullable ints". (so new List<int?>())

Julian
  • 33,915
  • 22
  • 119
  • 174
-1

Inserting into a list is adding another element to the list, at the specified location. This is not the same as setting the value at an index in an array. If you have an array with 100 items in it and set the value at index 3 you still have 100 items in the array. If you have a list of 100 items in it and you insert something at index 3 you now have a list with 101 items in it.

Jason Wadsworth
  • 8,059
  • 19
  • 32