我已经用 C++ 完成了它,但事实证明 Java 对我来说更具挑战性。这就是我所拥有的。我只是希望它有 4 行和 3 列初始化为 1-12 并将其打印到屏幕上。我的错误对你来说很明显吗?谢谢!
我得到 13 个错误 :(
including line9:twoDArray[][] not a statement, ; expected, illegal start of expression, all a few times each.
我试过的代码:
import java.util.*;
class twoDimensional array
{ public static void main(String args[])
{
int[][] twoDArray = new int[4][3];
twoDArray[][]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
System.out.print(twoDArray.toString);
}
}
最佳答案
首先,数组(甚至二维数组)不会覆盖 Object.toString
。您可以使用 Arrays.deepToString(Object[])
并在声明时初始化数组。有点像
int[][] twoDArray = new int[][] {
{ 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 }
};
System.out.println(Arrays.deepToString(twoDArray));
关于java - 如何在 Java 中创建一个简单的 4x3 二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35644633/