gpt4 book ai didi

c# - 定义一个没有固定大小的 double 组?

转载 作者:太空狗 更新时间:2023-10-29 19:54:33 25 4
gpt4 key购买 nike

你好,我有一个关于 C# 数组的问题。我需要一个数组来存储一些数据...我的代码是这样的

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

但是编译器说:not defined var如何定义没有固定大小的 double 组?非常感谢!

最佳答案

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

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};

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();

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

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]);
}

关于c# - 定义一个没有固定大小的 double 组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1026134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com