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
11 个解决方案
#1
65
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
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
列表(T)以获取更多信息
#2
12
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];
#3
5
You can't create an array without a size. You'd need to use a list for that.
没有大小就不能创建数组。你需要使用一个列表。
#4
4
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:
正如其他人提到的,您可以使用列表
String[] s = yourListOfString.ToArray();
#5
3
you can declare an empty array like below
您可以声明一个空数组,如下所示
String[] arr = new String[]{}; // declare an empty array
String[] arr2 = {"A", "B"}; // declare and assign values to an array
arr = arr2; // assign valued array to empty array
you can't assign values to above empty array like below
您不能将值赋给上面的空数组,如下所示。
arr[0] = "A"; // you can't do this
#6
1
I think you may be looking for the StringBuilder class. If not, then the generic List class in string form:
我想你可能正在寻找StringBuilder类。如果不是,则字符串形式的泛型列表类:
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类,项的顺序是实现细节,您不能保证它们将保持与放入它们的顺序相同。
#7
0
I suppose that the array size if a computed value.
假设数组的大小是一个计算值。
int size = ComputeArraySize();
// Then
String[] array = new String[size];
#8
0
Can you use a List strings and then when you are done use strings.ToArray() to get the array of strings to work with?
您是否可以使用一个列表字符串,然后在完成之后使用string . toarray()来获取要处理的字符串数组?
#9
0
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];
#10
0
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];
#11
-2
string foo = "Apple, Plum, Cherry";
苹果、李子、樱桃;
string[] myArr = null;
string[]myArr =零;
myArr = foo.Split(',');
myArr = foo.Split(" ");
#1
65
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
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
列表(T)以获取更多信息
#2
12
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];
#3
5
You can't create an array without a size. You'd need to use a list for that.
没有大小就不能创建数组。你需要使用一个列表。
#4
4
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:
正如其他人提到的,您可以使用列表
String[] s = yourListOfString.ToArray();
#5
3
you can declare an empty array like below
您可以声明一个空数组,如下所示
String[] arr = new String[]{}; // declare an empty array
String[] arr2 = {"A", "B"}; // declare and assign values to an array
arr = arr2; // assign valued array to empty array
you can't assign values to above empty array like below
您不能将值赋给上面的空数组,如下所示。
arr[0] = "A"; // you can't do this
#6
1
I think you may be looking for the StringBuilder class. If not, then the generic List class in string form:
我想你可能正在寻找StringBuilder类。如果不是,则字符串形式的泛型列表类:
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类,项的顺序是实现细节,您不能保证它们将保持与放入它们的顺序相同。
#7
0
I suppose that the array size if a computed value.
假设数组的大小是一个计算值。
int size = ComputeArraySize();
// Then
String[] array = new String[size];
#8
0
Can you use a List strings and then when you are done use strings.ToArray() to get the array of strings to work with?
您是否可以使用一个列表字符串,然后在完成之后使用string . toarray()来获取要处理的字符串数组?
#9
0
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];
#10
0
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];
#11
-2
string foo = "Apple, Plum, Cherry";
苹果、李子、樱桃;
string[] myArr = null;
string[]myArr =零;
myArr = foo.Split(',');
myArr = foo.Split(" ");