C#声明一个二维数组

5
我正在尝试在C#中设置一个二维数组,作为迷宫来移动角色,但我在初始化数组时遇到了一些问题。我想做以下操作:

但是 InitialiseMaze 方法提示迷宫未声明。

有人能给予建议吗?

谢谢。

Simon

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
}

}


我几乎可以确定在初始化期间应该有一个“new”在那里。 :) - Patryk Ćwiek
为什么不将这个放在一个文件中,然后从那里加载呢? - linkerro
不是与问题相关,但如果您将迷宫设计为字符串,例如“ #### ”,并使用#表示墙壁,使用空格表示空白空间,则设计迷宫会更容易。其他字符可以是迷宫中遇到的其他事物。然后在初始化时,只需将字符串拆分为int值并按原样存储即可。 - Pasi Savolainen
@linkerro - 如果他甚至不能声明一个数组,那么他肯定无法读取文件。此外,使用数组会更快。 - Security Hound
5个回答

10

除了在变量声明中,你无法像那样初始化一个数组。不过,修改很简单:

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

作为附言:

  • 看起来maze应该是一个实例变量而不是一个静态变量。毕竟,每次创建Maze实例时都要初始化它。
  • 你没有理由使用终结器。在C#中,极少需要(或者说根本不建议)使用终结器。

2

好的,以下是一些来自 msdn 的摘录:

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

本文内容摘自MSDN多维数组

你还应该了解析构函数、终结器等相关知识,我猜你是从C++过来的?两种语言之间的差异并不总是显而易见的:)


正如Jon Skeet所说,C#中很少需要使用finalizers(终结器)。我自己从未使用过它们。 - squelos

2

为了让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, 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}
};

小伙子,那是一个大迷宫数组...


0

你需要声明迷宫

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

1
不需要,这些值已经能够被推断出来了。只要 new int[,] { ... } 就可以了。 - Jon Skeet

0

二维数组

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

可以将二维数组看作是一个表格,它有x行和y列。以下是一个包含3行4列的二维数组示例:

enter image description here

C#中的二维数组

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

初始化二维数组

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接