C# 设置列表初始大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15843402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set List Initial Size
提问by Glimpse
How do I set the initial size of a list of a certain object type in .Net using C#, with the ability to insert fully allocated objects at a specified index?
如何使用 C# 在 .Net 中设置特定对象类型列表的初始大小,并能够在指定索引处插入完全分配的对象?
采纳答案by RBarryYoung
This can be easily done with an array:
这可以通过数组轻松完成:
string[] sa = new string[99];
sa[71] = "g";
Which also happens to implement the IList interface.
这也恰好实现了 IList 接口。
回答by pascalhein
You can use the constructor overloading List<T>(int capacity)
:
您可以使用构造函数重载List<T>(int capacity)
:
var l = new List<string>(42);
creates a list with a capacity of 42.
创建一个容量为 42 的列表。
回答by paparazzo
Down voters that claim Insert fails.
Did you read the documentation in the link?
ArgumentOutOfRangeException if index is less than 0 -or- index is greater than Count.
So capacity and count are not the same - does not make the answer wrong.
The answer from MarcinJuraszek will throw an ArgumentOutOfRangeException if index is less than 0 -or- index is greater than Count.
I use exactly this in a production application to load in alphabetic order and then insert any user additions at index 0 and it has neverthrown an exception.
声称插入失败的投票者失败。
您是否阅读了链接中的文档?
ArgumentOutOfRangeException 如果索引小于 0 - 或 - 索引大于 Count。
所以容量和数量是不一样的 - 不会使答案出错。
如果 index 小于 0 或 index 大于 Count,MarcinJuraszek 的答案将抛出 ArgumentOutOfRangeException。
我在生产应用程序中完全使用它按字母顺序加载,然后在索引 0 处插入任何用户添加,它从未引发异常。
回答by MarcinJuraszek
There is List<T>
constructor which takes an int
as a parameter for initiall list capacity, but it does not actually creates that number of elements within a list, so this will throw ArgumentOutOfRangeException
:
有一个List<T>
构造函数将 anint
作为初始列表容量的参数,但它实际上并没有在列表中创建该数量的元素,因此这将抛出ArgumentOutOfRangeException
:
var items = new List<int>(10);
items[4] = 3;
You can create your own method for creating that kind of List
, with initial size:
您可以创建自己的方法来创建那种List
具有初始大小的 :
private static List<T> CreateList<T>(int capacity)
{
return Enumerable.Repeat(default(T), capacity).ToList();
}
It'll make it work:
它会让它工作:
var items = CreateList<int>(10);
items[4] = 3;
However - why don't you just use Array
instead of List
when you know required capacity?
但是 - 为什么不使用Array
而不是List
在您知道所需容量时使用?
No-LINQ version
无 LINQ 版本
private static List<T> CreateList<T>(int capacity)
{
List<T> coll = new List<T>(capacity);
for(int i = 0; i < capacity; i++)
coll.Add(default(T));
return coll;
}
回答by LuiNova
To my knowledge setting an initial size to a list goes against the whole idea behind a List compared to an array.
据我所知,与数组相比,为列表设置初始大小与列表背后的整个想法背道而驰。
But here is how you do it:
但这是你如何做到的:
List<ItemType> list = new List<ItemType>(size);
size is int data type.
size 是 int 数据类型。