我正在努力
var test = new List< int>(10);
test.Insert(1, 0);
or
test[1] =0;
我在这方面遇到了异常(exception)。
如何插入列表?
谢谢 有足够的空间让这个编辑器正确显示内容。
最佳答案
var test = new List<int>(10);
test.Add(1);
或
var test = new List<int>(10);
test.Insert(0, 1) // inserts the value in the first position
更新
我错过了你的初衷。要做你想做的,你只需要用一个初始值初始化列表中的每个位置。不那么建议的方式是这样的:
//var test = new List<int>(10){0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
var test = new List<int>(10);
test.AddRange(Enumerable.Repeat(0, 10)); // Thanks to Ahmad
test[3] = 10; // works now
关于c# - .Net List<T> 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1488193/