C#中长度未知的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/599369/
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
Array of an unknown length in C#
提问by UnkwnTech
I've just started learning C# and in the introduction to arrays they showed how to establish a variable as an array but is seems that one must specify the length of the array at assignment, so what if I don't know the length of the array?
我刚开始学习 C#,在数组介绍中,他们展示了如何将变量建立为数组,但似乎必须在赋值时指定数组的长度,如果我不知道数组的长度怎么办?大批?
采纳答案by Samuel
回答by yfeldblum
var yummy = new List<string>();
while(person.FeelsHappy()) {
yummy.Add(person.GetNewFavoriteFood());
}
Console.WriteLine("Sweet! I have a list of size {0}.", list.Count);
Console.WriteLine("I didn't even need to know how big to make it " +
"until I finished making it!");
回答by mqp
You can create an array with the size set to a variable, i.e.
您可以创建一个大小设置为变量的数组,即
int size = 50;
string[] words = new string[size]; // contains 50 strings
However, that size can't change later on, if you decide you need 100 words. If you need the size to be really dynamic, you'll need to use a different sort of data structure. Try List.
但是,如果您决定需要 100 个单词,则该大小以后无法更改。如果您需要真正动态的大小,则需要使用不同类型的数据结构。试试List。
回答by Diego Jancic
Use an ArrayListif in .NET 1.x, or a List<yourtype>if in .NET 2.0 or 3.x.
ArrayList在 .NET 1.x 中使用if,或者List<yourtype>在 .NET 2.0 或 3.x 中使用if。
Search for them in System.Collectionsand System.Collections.Generics.
在System.Collections和 中搜索它们System.Collections.Generics。
回答by TFD
Use List<>to build up an 'array' of unknown length.
使用List<>建立一个未知的长度的“数组”。
Use List<>.ToArray()to return a real array, and not a List.
使用List<>.ToArray()返回真正的数组,而不是一个List。
var list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
var array = list.ToArray();
回答by Hannoun Yassir
try a generic list instead of array
尝试使用通用列表而不是数组
回答by Crash893
You might also want to look into Dictionarys if your data is unique, This will give you two columns to work with.
如果您的数据是唯一的,您可能还想查看字典,这将为您提供两列。
User name , Total bill
用户名,总账单
it gives you a lot of built in tools to search and update just the value.
它为您提供了许多内置工具来搜索和更新值。
回答by Novice
In a nutshell, please use Collections and Generics.
简而言之,请使用集合和泛型。
It's a must for any C# developer, it's worth spending time to learn :)
对于任何 C# 开发人员来说都是必须的,值得花时间学习 :)
回答by JulianR
A little background information:
一点背景资料:
As said, if you want to have a dynamic collection of things, use a List<T>. Internally, a List uses an array for storage too. That array has a fixed size just like any other array. Once an array is declared as having a size, it doesn't change. When you add an item to a List, it's added to the array. Initially, the Liststarts out with an array that I believe has a length of 16. When you try to add the 17th item to the List, what happens is that a new array is allocated, that's (I think) twice the size of the old one, so 32 items. Then the content of the old array is copied into the new array. So while a Listmay appear dynamic to the outside observer, internally it has to comply to the rules as well.
如前所述,如果您想拥有动态的事物集合,请使用List<T>. 在内部,List 也使用数组进行存储。该数组与任何其他数组一样具有固定大小。一旦数组被声明为具有大小,它就不会改变。当您将一个项目添加到 a 时List,它会被添加到数组中。最初,List从我认为长度为 16 的数组开始。当您尝试将第 17 项添加到 时List,会发生分配一个新数组的情况,这是(我认为)旧数组大小的两倍,所以 32 项。然后将旧数组的内容复制到新数组中。因此,虽然 aList在外部观察者看来可能是动态的,但在内部它也必须遵守规则。
And as you might have guessed, the copying and allocation of the arrays isn't free so one should aim to have as few of those as possible and to do that you can specify (in the constructor of List) an initial size of the array, which in a perfect scenario is just big enough to hold everything you want. However, this is micro-optimization and it's unlikely it will ever matter to you, but it's always nice to know what you're actuallydoing.
正如您可能已经猜到的那样,数组的复制和分配不是免费的,因此应该尽可能少地复制和分配,为此您可以指定(在 的构造函数中List)数组的初始大小,在完美的场景中,它刚好足以容纳您想要的一切。然而,这是微优化,它对您来说不太可能重要,但知道您实际在做什么总是很好。
回答by winwaed
As detailed above, the generic List<> is the best way of doing it.
如上所述,通用 List<> 是最好的方法。
If you're stuck in .NET 1.*, then you will have to use the ArrayList class instead. This does not have compile-time type checking and you also have to add casting - messy.
如果您坚持使用 .NET 1.*,那么您将不得不改用 ArrayList 类。这没有编译时类型检查,您还必须添加强制转换 - 混乱。
Successive versions have also implemented various variations - including thread safe variants.
后续版本还实现了各种变体——包括线程安全变体。

