Java 如何将循环长度设置为2D数组长度?
我有java代码,可以对内部循环的长度执行此操作Java 如何将循环长度设置为2D数组长度?,java,Java,我有java代码,可以对内部循环的长度执行此操作 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 } } char[]board=新字符[3][3]; 对于(int a=0;a
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
}
}
char[]board=新字符[3][3];
对于(int a=0;a
但当我在c#as中尝试时
for(intb=0;b
它会产生错误
“语法错误:应为值”
如果我把这个值写下来
for(int b = 0; b < board[a,b].Length; b++)
for(intb=0;b
它会产生错误
char不包含长度的定义
有人能帮我吗
试试这个
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
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
char[,]board={{{'a','b','c'},{'d','e','f'},{'g','h','i'};
对于(inta=0;a
您可以使用GetLength方法。
请参见您是采用board[]还是board[,]格式的c#数组?@jdweng its char[,]board=board[3,3]我在您的帖子中添加了解决方案,因为我无法创建新答案,因为问题被标记为重复。
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
}
}
}
}
}