java数组分配问题
在使用方法Arrays.deepToString(Object[]a)时,我面临着这个问题,我可以用这种方式来解决这个问题java数组分配问题,java,arrays,Java,Arrays,在使用方法Arrays.deepToString(Object[]a)时,我面临着这个问题,我可以用这种方式来解决这个问题 Object [] not_allowed = new int[]{7, 9, 8}; Object [] allowed = new int[][]{{1, 2, 3}, {6, 5, 4}}; 为什么在一维情况下不允许我将int[]分配给对象[],但在多维情况下允许我这样做 ints不是Objects,这就是为什么第一个赋值是非法的:一个ints数组不是一个O
Object [] not_allowed = new int[]{7, 9, 8};
Object [] allowed = new int[][]{{1, 2, 3}, {6, 5, 4}};
为什么在一维情况下不允许我将int[]分配给对象[],但在多维情况下允许我这样做
int
s不是Object
s,这就是为什么第一个赋值是非法的:一个int
s数组不是一个Object
s数组
但是数组,不管是什么,都是对象。您正在成功地将外部数据结构(一个数组数组)分配给一个
对象数组。int
s不是对象
s,这就是为什么第一次分配是非法的:一个int
s数组不是对象
s数组
但是数组,不管是什么,都是对象。您正在成功地将外部数据结构(一个数组数组)分配给对象的数组。小写int是一个原语,不从对象继承。尝试改用整数。小写int是一个基元,不从对象继承。尝试改用整数。第一个赋值不起作用,因为int[]的元素类型是int,而不是Object。因为(您正试图分配给Object[],所以元素类型应该是Object,而不是int)
第二个赋值不起作用,因为int[]是一个对象,因此int[]]是一个对象[]。第一个赋值不起作用,因为int[]的元素类型是int,而不是Object。因为(您正试图分配给Object[],所以元素类型应该是Object,而不是int)
第二个赋值不起作用,因为int[]是一个对象,因此int[][]是一个对象[]。像int
这样的基本类型不是对象,而数组是一个对象——您可以将任何数组赋值给对象引用
Object o = new int[]{7, 9, 8};
newint[][]
是一个对象数组,因此可以分配给Object[]
。您可能希望编写一个类似这样的实用程序方法来执行您想要的操作:
public static String arrayToString(Object o) {
if(o instanceof Object[]) {
return Arrays.deepToString((Object[]) o);
}
else if(o instanceof long[]) {
return Arrays.toString((long[]) o);
}
else if(o instanceof int[]) {
return Arrays.toString((int[]) o);
}
else if(o instanceof short[]) {
return Arrays.toString((short[]) o);
}
else if(o instanceof byte[]) {
return Arrays.toString((byte[]) o);
}
else if(o instanceof float[]) {
return Arrays.toString((float[]) o);
}
else if(o instanceof double[]) {
return Arrays.toString((double[]) o);
}
else if(o instanceof boolean[]) {
return Arrays.toString((boolean[]) o);
}
throw new IllegalArgumentException("input is not an array");
}
例如:
Object intArray = new int[]{7, 9, 8};
Object[] intintArray = new int[][]{{1, 2, 3}, {6, 5, 4}};
Object[] intintintArray = new int[][][]{{{1, 2, 3}, {6, 5, 4}},
{{1, 2, 3}, {6, 5, 4}}};
System.out.println(arrayToString(intArray));
System.out.println(arrayToString(intintArray));
System.out.println(arrayToString(intintintArray));
输出:
[7, 9, 8]
[[1, 2, 3], [6, 5, 4]]
[[[1, 2, 3], [6, 5, 4]], [[1, 2, 3], [6, 5, 4]]]
当数组是对象时,像int
这样的基本类型不是对象——您可以将任何数组指定给对象引用
Object o = new int[]{7, 9, 8};
newint[][]
是一个对象数组,因此可以分配给Object[]
。您可能希望编写一个类似这样的实用程序方法来执行您想要的操作:
public static String arrayToString(Object o) {
if(o instanceof Object[]) {
return Arrays.deepToString((Object[]) o);
}
else if(o instanceof long[]) {
return Arrays.toString((long[]) o);
}
else if(o instanceof int[]) {
return Arrays.toString((int[]) o);
}
else if(o instanceof short[]) {
return Arrays.toString((short[]) o);
}
else if(o instanceof byte[]) {
return Arrays.toString((byte[]) o);
}
else if(o instanceof float[]) {
return Arrays.toString((float[]) o);
}
else if(o instanceof double[]) {
return Arrays.toString((double[]) o);
}
else if(o instanceof boolean[]) {
return Arrays.toString((boolean[]) o);
}
throw new IllegalArgumentException("input is not an array");
}
例如:
Object intArray = new int[]{7, 9, 8};
Object[] intintArray = new int[][]{{1, 2, 3}, {6, 5, 4}};
Object[] intintintArray = new int[][][]{{{1, 2, 3}, {6, 5, 4}},
{{1, 2, 3}, {6, 5, 4}}};
System.out.println(arrayToString(intArray));
System.out.println(arrayToString(intintArray));
System.out.println(arrayToString(intintintArray));
输出:
[7, 9, 8]
[[1, 2, 3], [6, 5, 4]]
[[[1, 2, 3], [6, 5, 4]], [[1, 2, 3], [6, 5, 4]]]
您的第一行无效:
Object [] not_allowed = new int[]{7, 9, 8};
这是因为int
不是对象。这将是有效的:
Object allowed = new int[]{7, 9, 8};
您的第一行无效:
Object [] not_allowed = new int[]{7, 9, 8};
这是因为int
不是对象。这将是有效的:
Object allowed = new int[]{7, 9, 8};
另外一点要提到的是,允许
Object [] allowed = new int[][]{{1, 2, 3}, {6, 5, 4}};
实际上也不应该这样,因为你应该能够用
allowed[0] = "Surprise!";
因此,设法将字符串
放入一个数组中,该数组应该只包含int[]
s。这就是为什么Java必须在运行时检查对数组元素的每个分配,以确保分配的元素与数组的实际类型兼容,而不仅仅是让编译时类型检查来处理它。因此,上面的代码可以编译,但在尝试分配字符串时会导致异常
此问题也仅影响阵列。当泛型被添加到Java中时,设计者更清楚地知道,在泛型集合中,int[]
的集合不是Object
集合的子类型,因此,在您的示例中,如果您从数组切换到泛型ArrayList
s,这两行都不会编译。另外一点需要提及的是允许编译的行
Object [] allowed = new int[][]{{1, 2, 3}, {6, 5, 4}};
实际上也不应该这样,因为你应该能够用
allowed[0] = "Surprise!";
因此,设法将字符串
放入一个数组中,该数组应该只包含int[]
s。这就是为什么Java必须在运行时检查对数组元素的每个分配,以确保分配的元素与数组的实际类型兼容,而不仅仅是让编译时类型检查来处理它。因此,上面的代码可以编译,但在尝试分配字符串时会导致异常
此问题也仅影响阵列。当泛型被添加到Java中时,设计者更清楚地知道,在泛型集合中,int[]
的集合不是Object
集合的子类型,因此,在您的示例中,如果从数组切换到泛型ArrayList
s,那么这两行都不会编译。int[]
是一个对象
,没错,但这并不意味着int[][]
是一个对象[]
,因为您可以将无法插入int[][]
的项插入对象
。好的,同意。也许我应该写“int[][]是Object[]的一个特例”,而不是说“int[][]是Object[]”,int[]
是一个对象
,没错,但这并不意味着int[]
是一个对象[]
,因为可以将无法插入int[]的项插入对象[]
。好的,同意。也许我应该写“int[][]是Object[]的一个特例”,而不是说“int[][]是Object[]”