我很有趣学习如何仅使用本机纯 java 实现 2d String [] [] 数组控制台打印输出。以下代码在我运行程序时打印出一个二维矩阵,但是在将我的字符串转换为二维数组时进行一些测试运行时,一个特定的行令人困惑:
rulesTable[i][j] = rulesMatrix[j];
有没有一种方法可以编码,其中 rulesMatrix 将 = 到 2d 字符串数组,而不是我目前拥有的 1d 字符串数组?任何提示都会很棒。谢谢你。以下代码段包含有问题的循环:
protected void forgeGrammar()
{
//this verifies that all 5 rules displayed in the console are valid
grammarRules = new String(startingRule + secondRule + thirdRule + fourthRule + fifthRule);
rulesArray = new String[grammarRules.length()];
//remove the commas from the grammar
rulesArray = grammarRules.split("[,]");
//convert the grammar to a 2D String array
rulesTable = new String[rulesArray.length][5];
for (int i = 0; i < rulesTable.length; i++)
{
String[] rulesMatrix = rulesArray[i].split(blank);
for(int j = 0; j < rulesMatrix.length; j++)
{
rulesTable[i][j] = rulesMatrix[j];
}
}