2016-01-21 128 views
0

我有java代码在那里我可以为内环的长度如何设置等于2D阵列长度的回路长度?

char[][] board = new char[3][3]; 

for(int a = 0; a < board.length; a++) 
{ 
    for(int b = 0; b < board[a].length; b++) 
    { 
     //my code here 
    } 
} 

做到这一点,但是当我尝试在C#作为

for(int b = 0; b < board[a,].Length; b++) 

它生成错误

"Syntax error: value expected"

,如果我把的价值,并写下它像

for(int b = 0; b < board[a,b].Length; b++) 

它产生错误

char does not contain a definition for Length

有人可以帮我吗?

试试这个

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      char[,] board = { { 'a', 'b', 'c' }, { 'd', 'e', 'f' }, { 'g', 'h', 'i' } }; 

      for (int a = 0; a < board.GetLength(0); a++) 
      { 
       for (int b = 0; b < board.GetLength(1); b++) 
       { 
        char test = board[a,b]; 
        //my code here 
       } 
      } 
     } 
    } 
} 
+0

您是c#数组,格式为board [] []还是board [,]? – jdweng

+0

@jdweng其char [,] board = board [3,3] – Masu

+0

我将解决方案添加到您的帖子中,因为我无法创建新的答案,因为问题被标记为重复。 – jdweng

回答

0

可以使用对GetLength方法。 请参阅here