【问题标题】:Array in another Array C# [closed]另一个数组 C# 中的数组 [关闭]
【发布时间】:2020-01-05 21:15:05
【问题描述】:

我在 C# 中创建代码,但无法在另一个中创建数组。

int[, , ,] linee = new int[4, 4, 4, 4];


int[] line1 = new int[] { 10, 50, 150, 50 };
int[] line2 = new int[] { 10, 50, 10, 100 };
int[] line3 = new int[] { 10, 100, 150, 100 };
int[] line4 = new int[] { 150, 50, 150, 100 };

linee[0] = line1;

它给了我错误:

错误 CS0022 [] 中的索引数不正确。

(在最后一行)

【问题讨论】:

  • 什么错误?我们无法猜测,或者至少我们不想。
  • 错误 CS0022 [] 中的索引数不正确。 (在最后一行)
  • 你在搜索jagged arrays吗?
  • 我想要一个多维数组,每个维度都有一个数组
  • 如果您向我们展示了您将使用此数组的用途,那真的会有所帮助。

标签: c# arrays vector int


【解决方案1】:

您将multi-dimensional arraysjagged arrays 或数组数组混淆了。

您正在声明一个多维数组,但随后您尝试将其分配为锯齿状数组。

对于多维数组,您需要像这样单独分配值:

int[,,,] linee = new int[4, 4, 4, 4];

linee[0, 0, 0, 0] = 10;
linee[0, 0, 0, 1] = 50;
linee[0, 0, 0, 2] = 150;
linee[0, 0, 0, 3] = 50;

对于锯齿状数组,您可以按照您尝试的方式分配现有数组:

int[][][][] jaggedArray = new int[4][][][];

int[] line1 = new int[] { 10, 50, 150, 50 };
jaggedArray[0][0][0] = line1;

【讨论】:

    【解决方案2】:

    对于 C# 中的多维数组,您需要这样的东西

    int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                 { { 7, 8, 9 }, { 10, 11, 12 } } };
    

    【讨论】:

    • 这如何回答这个问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 2021-10-16
    相关资源
    最近更新 更多