C# 未知大小的字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1603599/
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 string with unknown size
提问by eriksv88
How is an array of string where you do not know where the array size in c#.NET?
c#.NET 中不知道数组大小的字符串数组如何?
String[] array = new String[]; // this does not work
采纳答案by Stan R.
Is there a specific reason why you need to use an array? If you don't know the size before hand you might want to use List<String>
您需要使用数组是否有特定原因?如果您事先不知道尺寸,您可能想要使用List<String>
List<String> list = new List<String>();
list.Add("Hello");
list.Add("world");
list.Add("!");
Console.WriteLine(list[2]);
Will give you an output of
会给你一个输出
!
MSDN - List(T)for more information
MSDN - List(T)了解更多信息
回答by sepp2k
You can't create an array without a size. You'd need to use a list for that.
您无法创建没有大小的数组。你需要为此使用一个列表。
回答by jasonh
I think you may be looking for the StringBuilderclass. If not, then the generic List class in string form:
我想您可能正在寻找StringBuilder类。如果没有,那么字符串形式的通用 List 类:
List<string> myStringList = new List<string();
myStringList.Add("Test 1");
myStringList.Add("Test 2");
Or, if you need to be absolutely sure that the strings remain in order:
或者,如果您需要绝对确保字符串保持有序:
Queue<string> myStringInOriginalOrder = new Queue<string();
myStringInOriginalOrder.Enqueue("Testing...");
myStringInOriginalOrder.Enqueue("1...");
myStringInOriginalOrder.Enqueue("2...");
myStringInOriginalOrder.Enqueue("3...");
Remember, with the List class, the order of the items is an implementation detail and you are not guaranteed that they will stay in the same order you put them in.
请记住,对于 List 类,项目的顺序是一个实现细节,您不能保证它们会保持与您放置它们的顺序相同。
回答by Vitaliy
I suppose that the array size if a computed value.
我想数组大小如果是一个计算值。
int size = ComputeArraySize();
// Then
String[] array = new String[size];
回答by Wil P
Can you use a List strings and then when you are done use strings.ToArray() to get the array of strings to work with?
您可以使用 List 字符串,然后在完成后使用 strings.ToArray() 来获取要使用的字符串数组吗?
回答by Andrew Hare
As others have mentioned you can use a List<String>
(which I agree would be a better choice). In the event that you need the String[]
(to pass to an existing method that requires it for instance) you can always retrieve an array from the list (which is a copy of the List<T>'s
inner array) like this:
正如其他人提到的,您可以使用 a List<String>
(我同意这将是更好的选择)。如果您需要String[]
(例如传递给需要它的现有方法),您始终可以从列表(它是List<T>'s
内部数组的副本)中检索一个数组,如下所示:
String[] s = yourListOfString.ToArray();
回答by Malfist
If you will later know the length of the array you can create the initial array like this:
如果您稍后会知道数组的长度,您可以像这样创建初始数组:
String[] array;
And later when you know the length you can finish initializing it like this
稍后当您知道长度时,您可以像这样完成初始化
array = new String[42];
回答by pmarflee
You don't have to specify the size of an array when you instantiate it.
实例化数组时不必指定数组的大小。
You can still declare the array and instantiate it later. For instance:
您仍然可以声明数组并稍后实例化它。例如:
string[] myArray;
...
myArray = new string[size];
回答by Kay
string foo = "Apple, Plum, Cherry";
string foo = "苹果、李子、樱桃";
string[] myArr = null;
string[] myArr = null;
myArr = foo.Split(',');
myArr = foo.Split(',');
回答by shreesha
If you want to use array without knowing the size first you have to declare it and later you can instantiate it like
如果你想在不知道大小的情况下使用数组,你必须先声明它,然后你可以像这样实例化它
string[] myArray;
...
...
myArray=new string[someItems.count];