我正在尝试打印二维数组中的元素,但似乎无法对其进行格式化。每当我尝试格式化它时,我都会收到错误
String [][] plants = new String[2][2];
plants[0][0] = "Rose";
plants[0][1] = "Red";
plants[1][0] = "Snowdrop";
plants[1][1] = "White";
//String plant;
//String color;
for (int i = 0; i<2; i++){
for (int j = 0; j<2; j++){
//plant = Arrays.toString(plants[i]);
//color = Arrays.deepToString(plants[j]);
//System.out.println(plant + " " + color);
System.out.println(plants[i][j]);
}
}
到目前为止,我在单独的一行上打印出每个元素,但我希望它像这样打印出来:
玫瑰红
雪花莲白
我已经尝试过注释掉的方法,但它们也不行。
有什么建议吗?谢谢
最佳答案
在内部循环中执行 System.out.print(plants[i][j] + "");
在外循环中执行System.out.println();
关于java - 在java中打印出二维数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16789504/