所以,我写了下面的代码,用从字符串文件中读取的字符填充多维板。例如,如果我的文件是:abc(新队)定义板子数组未填充:{{'a','b','c'},{'d','e','f'}}(板子的尺寸是[柜台,长度])
代码:
char[,] board = new char[counter, length];
int k = 0;
while (((line = file.ReadLine()) != null) && (k < counter))
{
char[] characters = line.ToCharArray();
int l = 0;
while (l < length)
{
foreach (char ch in characters)
{
board[k, l] = ch;
}
l++;
}
k++;
}
没有立即看到它,但是问题是while(l for循环迭代字符串中的字符,但在此迭代过程中, 您的代码的固定版本如下。 注意:字符串已经是l
不变。char
的序列,因此您不必将其转换为ToCharArray()。char[,] board = new char[counter, length];
int k = 0;
while (((line = file.ReadLine()) != null) && (k < counter))
{
int l = 0;
while (l < length)
{
// line is a string and we can access its characters with indexing
board[k, l] = line[l];
l++;
}
k++;
}