C# 定义一个没有固定大小的双数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1026134/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:50:02  来源:igfitidea点击:

Define a double array without a fixed size?

c#arrayslistdouble

提问by subprime

Hello i have a problem with c# Arrays. I need a array to store some data in there... My Code is that

您好,我在使用 c# 数组时遇到问题。我需要一个数组来存储一些数据......我的代码是

double[] ATmittelMin;
ATmittelMin[zaehlMittel] = Gradient(x, xATmax, y, yATmax);

But the compiler says: not defined var How can i define a double array without a fixed size ? Thanks a lot!

但是编译器说:没有定义 var 如何定义一个没有固定大小的双数组?非常感谢!

采纳答案by Blixt

Arrays are always fixed in size, and have to be defined like so:

数组的大小总是固定的,必须像这样定义:

double[] items1 = new double[10];

// This means array is double[3] and cannot be changed without redefining it.
double[] items2 = {1.23, 4.56, 7.89};

The List<T>class uses an array in the background and redefines it when it runs out of space:

所述List<T>类使用阵列中的背景和当它运行的空间出来重新定义它:

List<double> items = new List<double>();
items.Add(1.23);
items.Add(4.56);
items.Add(7.89);

// This will give you a double[3] array with the items of the list.
double[] itemsArray = items.ToArray();

You can iterate through a List<T>just as you would an array:

您可以像遍历List<T>数组一样遍历 a :

foreach (double item in items)
{
    Console.WriteLine(item);
}

// Note that the property is 'Count' rather than 'Length'
for (int i = 0; i < items.Count; i++)
{
    Console.WriteLine(items[i]);
}

回答by Daniel Schneller

From what I see you did not declare the zaehlMittelvariable. I guess this is what the compiler complains about.

据我所知,您没有声明zaehlMittel变量。我想这就是编译器所抱怨的。

Apart from that, even though you can of course determine the value of that variable programmatically, the moment you want to create an array its size must be known. This is the way arrays work.

除此之外,即使您当然可以以编程方式确定该变量的值,但在您想要创建数组时必须知道其大小。这就是数组的工作方式。

In case you cannot do that easily, I suggest using some sort of dynamic datastructure, like a list or a set. Then, once all elements have been added, you are of course still free to create an array from that, as by that time you know the number of elements (even though there are convenience methods like toArray()that will even take care of that).

如果您不能轻松做到这一点,我建议使用某种动态数据结构,例如列表或集合。然后,一旦添加了所有元素,您当然仍然可以自由地从中创建一个数组,因为到那时您就知道元素的数量(即使有类似的便捷方法toArray()甚至可以解决这个问题)。

回答by Julien Poulin

You have to instanciate the array before using it:

你必须在使用它之前实例化数组:

double[] ATmittelMin = new double[10];
ATmittelMin[zaehlMittel] = Gradient(x, xATmax, y, yATmax);

回答by ChrisF

The obvious solution that springs to mind is to use a List:

想到的显而易见的解决方案是使用列表:

List<double> ATmittelMin = new List<double>();
ATmittelMin.Add(Gradient(x, xATMax, y, yATMax);

But if you don't want to convert from a list to an array you can grow the array later:

但是,如果您不想从列表转换为数组,则可以稍后扩展数组:

double[] ATmittelMin = new double[10];
// Some code
int index = some_value;
if (index >= TmittelMin.Length)
{
    Array.Resize(ATmittelMin, index+5);  // So we're not constantly resizing the array
}

It's not ideal as you're doing a lot of the work that Listis doing behind the scenes - probably a lot more efficiently than you can.

这并不理想,因为您正在做很多List在幕后进行的工作- 可能比您更有效。