我試圖向List索引添加一個值。我想只增加索引的當前值,但是這個片段顯示了錯誤。遞增列表的索引值<int>
Random random = new Random();
int[] intArray = new int[10];
List<int> intList = new List<int>();
for (int i = 0; i < 10; i++)
{
int randomNumber = random.Next(1, 10);
intArray[randomNumber] = intArray[randomNumber] + randomNumber;
// The next line does not add the value of the random number to the List(randomNumber)
//System.ArgumentOutOfRangeException was unhandled
intList.Insert(randomNumber, intList[randomNumber]+randomNumber);
}
使用int []工作。