如何使用运行时确定的尺寸来初始化 C# 数组?

发布于 2024-09-14 09:53:00 字数 454 浏览 1 评论 0原文

感谢您的关注。

我有一个 2D c# 数组,其维度之一为 50。另一个维度取决于数据库中某处的行数,并在运行时决定。我将如何初始化这样的数组?

目前我对单行的初始化看起来像这样,但我确信有更好的方法来做到这一点,更有效:)

temp = new Double[50,1] { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},
                                {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},
                                {0},{0},{0},{0},{0},{0},{0},{0},{0},{0}};

and thanks for looking.

I have a 2D c# array, that has 50 as one of its dimensions. The other dimension depends on the number of rows in a database somewhere and is decided at runtime. How would I go about initializing an array like this?

Currently my initialization for a single row looks like this, but I'm sure there's a better way to do it, more efficiently :)

temp = new Double[50,1] { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},
                                {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},
                                {0},{0},{0},{0},{0},{0},{0},{0},{0},{0}};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

提笔书几行 2024-09-21 09:53:00

只需在运行时使用第二维的整数变量初始化数组即可。

double[,] = new double[50, v];

C# 会自动将所有双精度数初始化为零,因此在您的特定情况下,您不需要显式初始化这些值。

Simply initialize the array at runtime using an integer variable for the second dimension.

double[,] = new double[50, v];

C# will automatically initialize all doubles to zero, so in your specific circumstance, you don't need to explicitly initialize the values.

乖乖兔^ω^ 2024-09-21 09:53:00

正如托比所说,您不需要显式地将 double 值设置为零,因为 default(double) == 0.0

但是,如果您想将数组的所有成员初始化为数组类型默认值之外的某个值,您始终可以这样做:

static T[,] GetInitializedArray<T>(int width, int height, T value)
{
    var array = new T[width, height];
    for (int x = 0; x < width; ++x)
    {
        for (int y = 0; y < height; ++y)
        {
            array[x, y] = value;
        }
    }

    return array;
}

这将允许您编写如下代码:

double[,] temp = GetInitializedArray(50, 1, 0.0);

As Toby said, you don't need to explicity set double values to zero since default(double) == 0.0.

However, if you want to initialize all members of an array to some value other than the default for the array's type, you could always do this:

static T[,] GetInitializedArray<T>(int width, int height, T value)
{
    var array = new T[width, height];
    for (int x = 0; x < width; ++x)
    {
        for (int y = 0; y < height; ++y)
        {
            array[x, y] = value;
        }
    }

    return array;
}

This would allow you to write code such as this:

double[,] temp = GetInitializedArray(50, 1, 0.0);
吃兔兔 2024-09-21 09:53:00

如果数组的第二个维度可以变化,请使用锯齿状数组

double [][] temp = new double [50][]; 

知道您设置的数组暗淡的确切大小。

temp[index] = new double[length];

如果它始终具有矩形大小,请使用以下构造

double [,] temp = new double[50, length];

If a second dim of array can vary use jagged array

double [][] temp = new double [50][]; 

When you will know exact size of array dim you set it with.

temp[index] = new double[length];

If it always has rectangular size use following construct

double [,] temp = new double[50, length];
情深如许 2024-09-21 09:53:00
double[,] temp = new double[50,1];

double[][] temp = new double[50][];

有关数组的详细信息,请查看此处

double[,] temp = new double[50,1];

double[][] temp = new double[50][];

Take a look here for more information about arrays.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文