C# - float[][] declaration

Sha*_*ssy -7 c# multidimensional-array

I'm trying to declare this kind of variable:

float[][]
Run Code Online (Sandbox Code Playgroud)

Things that didn't work for me (wouldn't compile) -

float[][] inputs = new float[10][5];
float[][] inputs = new float[10,5];
Run Code Online (Sandbox Code Playgroud)

When trying to declare the array like this -

int a = 3;
int b = 2;
float[][] inputs = new float[][]
{
   new float[a],
   new float[b]
};
Run Code Online (Sandbox Code Playgroud)

I get a multidimensional array with two float arrays instead of an array that has 3 arrays and every array size is 2.

Dmi*_*nko 5

嗯,有两种不同的类型:

编辑:您的代码修改:

  // jagged array (10 arrays each of which has 5 items)
  float[][] inputs = new float[10][] {
    new float[5],
    new float[5],
    new float[5],
    new float[5],
    new float[5],
    new float[5],
    new float[5],
    new float[5],
    new float[5],
    new float[5],
  };
Run Code Online (Sandbox Code Playgroud)

您可以在Linq的帮助下缩短声明:

  float[][] inputs = Enumerable
    .Range(0, 10)               // 10 items
    .Select(i => new float[5])  // each of which is 5 items array of float
    .ToArray();                 // materialized as array
Run Code Online (Sandbox Code Playgroud)

或者在二维数组的情况下

  // 2d array 10x5
  float[,] inputs = new float[10,5];
Run Code Online (Sandbox Code Playgroud)