我把动态数组作为
double[] array = new double[10];
我在运行时添加了一些值,但是如果用户输入少于10个值,就像用户在其中放入5个值而不是10个。之后,当我首先显示数组时,它显示五个值,然后开始显示0 0 0 0 0.我只是想知道有没有什么方法可以摆脱在c#中使用动态数组?
double [10]不是动态数组。固定长度为10个条目。
请尝试使用List <double>。它实际上是动态的。
https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx
如果我打算正确,我想你可能正在寻找这样的东西:
var list = new List<double>();
list.Add(1.1);
list.Add(2.2);
list.Add(3.4);
list.Add(4.8);
list.Add(5.0);
var array = list.ToArray();
//here you can see that array has a length of 5
System.Diagnostics.Debug.WriteLine($"My array contains {array.Length}.");