初始化2D数组

时间:2015-10-08 19:11:08

标签: arrays vb.net chess

我正在尝试创建一个数组,其中每个元素中的2个字符代码对应于棋盘上的棋子。但是,我无法弄清楚如何使用2D数组。我将pieceArray声明为:

Public pieceArray(7,7)as String

然后我尝试使用这种方法填充它:

pieceArray =      {"BR", "Bk", "BB", "BQ", "BK", "BB", "Bk", "BR",
                   "BP", "BP", "BP", "BP", "BP", "BP", "BP", "BP",
                   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                   "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ",
                   "WP", "WP", "WP", "WP", "WP", "WP", "WP", "WP",
                   "WR", "Wk", "WB", "WQ", "WK", "WB", "Wk", "WR"}

但是这个方法给我一个尺寸不匹配的错误,所以请你解释一下如何正确地做到这一点?

由于

1 个答案:

答案 0 :(得分:2)

嗯,这不是你如何访问2D数组的读/写数据。它看起来更像是:

' an array with 5 rows and 2 columns
Dim a(4,1) As Integer

硬编码你的方式就像这样:

Module arrayApl
   Sub Main()
      ' an array with 5 rows and 2 columns
      Dim a(,) As Integer = {{0, 0}, {1, 2}, {2, 4}, {3, 6}, {4, 8}}
      Dim i, j As Integer
      ' output each array element's value '
      For i = 0 To 4
          For j = 0 To 1
              Console.WriteLine("a[{0},{1}] = {2}", i, j, a(i, j))
          Next j
      Next i
      Console.ReadKey()
   End Sub
End Module

也许take a look at a tutorial