【问题标题】:How to create a array in 2D array如何在二维数组中创建数组
【发布时间】:2011-10-04 07:55:48
【问题描述】:

我是 C# 新手,我正在尝试在二维数组中创建一个简单数组,Em 尝试以下代码但出现错误,

float [,] Tile = new float[17,23];
Tile[0,0] = new float[2] {1,2};

出现错误:无法隐式转换类型 float[]' tofloat'

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    Tile[0,0] 是一个浮点数。

    所以你应该像这样添加它

    float [,] Tile = new float[17,23];
    Tile[0,0] = 1;
    Tile[0,1] = 2;
    Tile[1,1] = 1337;
    etc..
    

    编辑 从你的评论中你可以做这样的事情

     float [,][] Tile = new float[17, 23][];
     Tile [0,0] = new float[] {1,2};
    

    【讨论】:

    • 但我想在该 2array 索引中创建一个数组。例如。 Tile[0,0] = 具有 2 个索引的数组
    • 你应该有new float而不是float float
    【解决方案2】:

    这是正确的代码:

     float[,][] Tile = new float[17, 23][];
     Tile[0, 0] = new float[2] { 1, 2 };
    

    有关 C# 数组的更多信息,请访问 http://msdn.microsoft.com/en-us/library/2s05feca.aspx

    【讨论】:

      【解决方案3】:

      我不确定您要在这里实现什么,但您的代码应该是这样的:

          float[,] Tile = new float[17, 23];
          Tile[0, 0] = 1.0f;
          Tile[0, 1] = 2.0f;
      

      【讨论】:

        【解决方案4】:

        尝试以下方法:

        float [,] Tile = new float[17,23];
        Tile[0,0] = 2;
        

        【讨论】:

          猜你喜欢
          • 2011-12-23
          • 1970-01-01
          • 2011-06-28
          • 2018-10-02
          • 2013-05-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多