我意识到如果我的列表是空的,我不能在第三个索引处插入一些东西,因为它会抛出越界错误。
List<int> list = new List<int>();
list.Insert(3, 1000); //System.ArgumentOutOfRangeException
Run Code Online (Sandbox Code Playgroud)
我将不得不用 null 填充第一个和第二个元素,以便我能够在第三个索引处插入一些东西。这似乎没有必要。但是如果它是一个数组,即使没有分配以前的索引,我也可以向我想要的任何索引插入一些东西。
int[] arr = new int[100];
arr[3] = 1000; //no error
Run Code Online (Sandbox Code Playgroud)
有什么方法可以在不填充前一个索引的情况下插入特定索引处的空列表?或者有其他更好的收藏吗?