假设
Object[][]a; //a matrix of Objects
Exception[] r; //an array of Exceptions
(注意异常是对象的子类(
现在,当
a = r;
在爪哇中合法吗?
不,您需要将其分配给 2D 数组中的特定索引,并确保数组初始化为:
Object[][] a = new Object[5][5];
Exception[] r = new Exception[5];
a[0] = r;
由于一切都是 Object 的子类,只要赋值的维度匹配,您就可以在 2d 数组中存储任何内容。这意味着您将能够执行以下操作:
a = r => if r is a 2d array as well
a[index] = r => if r is a 1d array
a[index-1][index-2] = r => if r is any type extending Object
在Java中,数组是对象的类型。所以你可以做到:
Object a = null;
Object[][][] b = null;
a = b;
这也允许:
Object a[] = null;
Object[][][][] b = null;
a = b
但你永远不能反过来做。