C#声明一个2D数组

时间:2012-03-09 15:11:03

标签: c# arrays multidimensional-array

我正在尝试在C#中设置一个2d数组作为迷宫来移动一个角色,我有几个问题初始化阵列,我正在尝试做下面的

但是InitialiseMaze方法说迷宫没有被宣布

任何人都可以提供建议

感谢

西蒙

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameMan
{
    public class Maze
    {
       #region Variables
       static int[,] maze;

    #endregion
    #region Constructors/Destructors
    public Maze()
    {
        InitaliseMaze();
    }
    ~Maze()
    {
    }
    #endregion

    #region Methods
    public void InitaliseMaze()
    {

         maze = {
                          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},    
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},     
                          {0, 0, 3, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0, 3, 0, 0},     
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},    
                          {0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 0},    
                          {0, 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0},     
                          {0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0},    
                          {0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 4, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0},     
                          {0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0},     
                          {1, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1, 1},
                          {0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0},
                          {0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 1, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0},
                          {0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0},
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
                          {0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0 ,0, 2, 0, 0},
                          {0, 0, 3, 2, 0, 2, 2, 2, 2, 2, 5, 2, 2, 2, 2, 2, 0, 2, 3, 0, 0},
                          {0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0},
                          {0, 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0},
                          {0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0},
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
                          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
                      };
    }
    #endregion
}

}

5 个答案:

答案 0 :(得分:6)

除了在变量声明中,您不能初始化数组。但是,改变很简单:

maze = new int[,] { 
   // As before
};

as asides:

  • 看起来maze应该是实例变量而不是静态变量。毕竟,每次创建Maze
  • 的实例时,都会对其进行初始化
  • 你无缘无故终结。终结者在C#
  • 中很少需要(或确实可取)

答案 1 :(得分:3)

让Jon的帖子更清晰一点:

maze = new int[,]{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},    
{0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},     
{0,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, 0},
{0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0},
{0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};

男孩,那是一个很大的迷宫阵......

答案 2 :(得分:1)

好的,这里有一些来自msdn的摘录:

 int[,] myArray = {{1,2}, {3,4}, {5,6}, {7,8}};

摘自MSDN multidimensional arrays

你还应该阅读有关Destructors,finalizers等的内容......,我打赌你来自C ++?两种语言之间的差异并不总是很明显:)。

答案 3 :(得分:0)

你需要申报迷宫

 numbers = new int[X,Y]; where X and Y are how big it is

答案 4 :(得分:0)

二维数组

多维数组的最简单形式是二维数组。二维数组是一维数组的列表。

可以将二维数组视为一个表,该表具有x的行数和y的列数。以下是一个二维数组,其中包含3行4列-

enter image description here

C#中的二维数组

因此,数组a中的每个元素都由a [i,j]形式的元素名称标识,其中a是数组的名称,而i和j是唯一标识数组中每个元素的下标

初始化二维数组

int [,] a = new int [3,4] {

   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */

   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */

   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */

};

解释上面的代码:

new int [**3**,4] **3** denoting to rows like how may object in array 

eg:

{0, 1, 2, 3} ,  
{4, 5, 6, 7} ,  
{8, 9, 10, 11}


new int [3,**4**] **4** denoting to columns like total value in object (4 columns)

eg:   


   {0, 1, 2, 3}

让我们检查程序来处理二维数组

 using System;
    namespace ArrayApplication {
       class MyArray {
          static void Main(string[] args) {
             /* an array with 5 rows and 2 columns*/
             int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} };
             int i, j;

             /* output each array element's value */
             for (i = 0; i < 5; i++) {

                for (j = 0; j < 2; j++) {
                   Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
                }
             }
             Console.ReadKey();
          }
       }
    }

Output:  

a[0,0]: 0
a[0,1]: 0
a[1,0]: 1
a[1,1]: 2
a[2,0]: 2
a[2,1]: 4
a[3,0]: 3
a[3,1]: 6
a[4,0]: 4
a[4,1]: 8