Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
要创建一个长度为 5 的 int 数组,我们使用以下语法:
int[] x = new int[5]
要创建一个二维数组,一个 int 数组的数组,我们说:
int[][] x = new int[5][];
这将创建一个长度为 5 的数组,该数组可以保存 int[] 对象。
对于第二种情况,为什么语法不是 this: ?
int[][] x = new int[][5]
毕竟,5 定义了我们可以拥有多少个 int 数组。不是我们要放入 x 的 int 数组的大小。
声明的数组带来是第一个维度,因此声明引用的数组大小对分配实际数组没有任何作用。这将类似于编码:
int[] a = new int[];
查找索引与构造索引不同会很奇怪。因此,如果您有int[][] x= new int[][5],那么您将使用 查找元素x[0..4][foo],这比替代方案更令人困惑。
int[][] x= new int[][5]
x[0..4][foo]