Java中的多维数组扩展了哪个类?

时间:2022-02-19 21:33:29

I need to know which class multidimensional arrays in Java extends exactly?

我需要知道Java中哪些类多维数组完全延伸?

When we assign

当我们分配

Object[] ref=new int[]{1,2,3};

the compiler complains that the objects are of different types. So it seems that one dimensional arrays extend Object; I know that already.

编译器抱怨对象是不同类型的。所以似乎一维数组扩展了Object;我已经知道了。

But when we assign

但是当我们分配时

Object[] ref2=new int[][]{{1,2,3},{4,5,6}};

the compiler will not complain. So it seems that two dimensional arrays extend Object[].

编译器不会抱怨。所以似乎二维数组扩展了Object []。

But when I print its superclass name:

但是当我打印它的超类名称时:

System.out.println(ref2.getClass().getSuperclass().getName());

I got java.lang.Object.

我有java.lang.Object。

So can anyone explain what's going on here?

所以有人能解释这里发生了什么吗?

4 个解决方案

#1


13  

A multidimensional array in Java is really just an array of arrays (of arrays)* .

Java中的多维数组实际上只是数组(数组)*的数组。

Also, arrays are considered subclasses of Object.

此外,数组被视为Object的子类。

So, your int[][] is an Object[] (with component type int[]), and also an Object (because all arrays are objects)

所以,你的int [] []是一个Object [](组件类型为int []),还有一个Object(因为所有数组都是对象)

An int[] however is not an Object[] (but it is still an Object).

然而,int []不是Object [](但它仍然是Object)。

So it seems that two dimensional arrays extend Object[]

所以似乎二维数组扩展了Object []

I am not sure if "extend" is the proper word here. Arrays have a special place in the Java type system, and work a little different from other objects. A two dimensional array is definitely an Object[]. But if you are asking about superclasses, the only superclass that any kind of array has is Object. All arrays are also Cloneable and Serializable.

我不确定“延伸”是否适用于此。数组在Java类型系统中有一个特殊的位置,与其他对象的工作方式略有不同。二维数组肯定是Object []。但是如果你问的是超类,那么任何一种数组都有的唯一超类是Object。所有数组也都是Cloneable和Serializable。

#2


3  

Your inheritance tree looks something like this:

你的继承树看起来像这样:

  1. ref2 is-a int[][]
  2. ref2是-int [] []
  3. ref2 is-a Object[]
  4. ref2是一个对象[]
  5. ref2 is-a Object
  6. ref2是一个对象

Here's a code fragment that illustrates what I mean:

这是一段代码片段,说明了我的意思:

Object ref2 = new int[][]{{1,2,3}, {4,5,6}};
System.err.println("ref2: " + (ref2 instanceof int[][]) + 
  " " + (ref2 instanceof Object[]));

You should see something like:

你应该看到类似的东西:

ref2: true true

#3


2  

Arrays in Java are covariant. This means that TSub[] is a subtype of TSuper[] if TSub is a subtype of TSuper.

Java中的数组是协变的。这意味着如果TSub是TSuper的子类型,则TSub []是TSuper []的子类型。

You have int[][] which is an array of int[]. Now, as others have pointed out, any array in Java is a subtype of Object, so int[] is a subtype of Object. So, due to array covariance, int[][] is a subtype of Object[] (substitute TSub = int[] and TSuper = Object in the above definition of covariance).

你有int [] []这是一个int []数组。现在,正如其他人所指出的,Java中的任何数组都是Object的子类型,因此int []是Object的子类型。因此,由于数组协方差,int [] []是Object []的子类型(在上面的协方差定义中替换TSub = int []和TSuper = Object)。

Edit - To make it clear why covariance is important here, consider that doing the same thing with List<T> wouldn't work:

编辑 - 为了弄清楚为什么协方差在这里很重要,请考虑使用List 做同样的事情是行不通的:

List<Object> ref2 = new List<int[]>()

#4


1  

When we assign

当我们分配

Object[] ref=new int[]{1,2,3};

the compiler complains

编译器抱怨

That's because int is not a subtype of Object, int.class.getSuperclass() returns null. Remember that in Java, primitive values (ints, longs, doubles, ...) are not objects.

那是因为int不是Object的子类型,int.class.getSuperclass()返回null。请记住,在Java中,原始值(整数,长整数,双精度数等)不是对象。

So it seems that two dimensional arrays extend Object[].
But when I print its superclass name:

所以似乎二维数组扩展了Object []。但是当我打印它的超类名称时:

 System.out.println(ref2.getClass().getSuperclass().getName());  

I got java.lang.Object.

我有java.lang.Object。

Arrays are more like interfaces, as they do multiple inheritance. But they are no real interfaces, in the sense of a Java interface.

数组更像是接口,因为它们可以进行多重继承。但就Java接口而言,它们并不是真正的接口。

class A {}
interface I {}
class B extends A implements I {}


B[][] bArray = new B[1][1];
System.out.println(bArray instanceof A[][]);
System.out.println(bArray instanceof I[][]);
System.out.println(bArray instanceof Object[]);
System.out.println(bArray.getClass().getSuperclass());
for (Class i: bArray.getClass().getInterfaces())
    System.out.println(i);
System.out.println(I[][].class.isAssignableFrom(bArray.getClass()));
System.out.println(I[][].class.isInstance(bArray));

Output:

输出:

true
true
true
class java.lang.Object
interface java.lang.Cloneable
interface java.io.Serializable
true
true

Furthermore, Java violates the Liskov substitution principle, because

此外,Java违反了Liskov替换原则,因为

B[] bArray = new B[1];
A[] aArray = bArray;
// bArray[0] = new A(); // causes a compile error
aArray[0] = new A(); // compiles, but causes runtime exception

#1


13  

A multidimensional array in Java is really just an array of arrays (of arrays)* .

Java中的多维数组实际上只是数组(数组)*的数组。

Also, arrays are considered subclasses of Object.

此外,数组被视为Object的子类。

So, your int[][] is an Object[] (with component type int[]), and also an Object (because all arrays are objects)

所以,你的int [] []是一个Object [](组件类型为int []),还有一个Object(因为所有数组都是对象)

An int[] however is not an Object[] (but it is still an Object).

然而,int []不是Object [](但它仍然是Object)。

So it seems that two dimensional arrays extend Object[]

所以似乎二维数组扩展了Object []

I am not sure if "extend" is the proper word here. Arrays have a special place in the Java type system, and work a little different from other objects. A two dimensional array is definitely an Object[]. But if you are asking about superclasses, the only superclass that any kind of array has is Object. All arrays are also Cloneable and Serializable.

我不确定“延伸”是否适用于此。数组在Java类型系统中有一个特殊的位置,与其他对象的工作方式略有不同。二维数组肯定是Object []。但是如果你问的是超类,那么任何一种数组都有的唯一超类是Object。所有数组也都是Cloneable和Serializable。

#2


3  

Your inheritance tree looks something like this:

你的继承树看起来像这样:

  1. ref2 is-a int[][]
  2. ref2是-int [] []
  3. ref2 is-a Object[]
  4. ref2是一个对象[]
  5. ref2 is-a Object
  6. ref2是一个对象

Here's a code fragment that illustrates what I mean:

这是一段代码片段,说明了我的意思:

Object ref2 = new int[][]{{1,2,3}, {4,5,6}};
System.err.println("ref2: " + (ref2 instanceof int[][]) + 
  " " + (ref2 instanceof Object[]));

You should see something like:

你应该看到类似的东西:

ref2: true true

#3


2  

Arrays in Java are covariant. This means that TSub[] is a subtype of TSuper[] if TSub is a subtype of TSuper.

Java中的数组是协变的。这意味着如果TSub是TSuper的子类型,则TSub []是TSuper []的子类型。

You have int[][] which is an array of int[]. Now, as others have pointed out, any array in Java is a subtype of Object, so int[] is a subtype of Object. So, due to array covariance, int[][] is a subtype of Object[] (substitute TSub = int[] and TSuper = Object in the above definition of covariance).

你有int [] []这是一个int []数组。现在,正如其他人所指出的,Java中的任何数组都是Object的子类型,因此int []是Object的子类型。因此,由于数组协方差,int [] []是Object []的子类型(在上面的协方差定义中替换TSub = int []和TSuper = Object)。

Edit - To make it clear why covariance is important here, consider that doing the same thing with List<T> wouldn't work:

编辑 - 为了弄清楚为什么协方差在这里很重要,请考虑使用List 做同样的事情是行不通的:

List<Object> ref2 = new List<int[]>()

#4


1  

When we assign

当我们分配

Object[] ref=new int[]{1,2,3};

the compiler complains

编译器抱怨

That's because int is not a subtype of Object, int.class.getSuperclass() returns null. Remember that in Java, primitive values (ints, longs, doubles, ...) are not objects.

那是因为int不是Object的子类型,int.class.getSuperclass()返回null。请记住,在Java中,原始值(整数,长整数,双精度数等)不是对象。

So it seems that two dimensional arrays extend Object[].
But when I print its superclass name:

所以似乎二维数组扩展了Object []。但是当我打印它的超类名称时:

 System.out.println(ref2.getClass().getSuperclass().getName());  

I got java.lang.Object.

我有java.lang.Object。

Arrays are more like interfaces, as they do multiple inheritance. But they are no real interfaces, in the sense of a Java interface.

数组更像是接口,因为它们可以进行多重继承。但就Java接口而言,它们并不是真正的接口。

class A {}
interface I {}
class B extends A implements I {}


B[][] bArray = new B[1][1];
System.out.println(bArray instanceof A[][]);
System.out.println(bArray instanceof I[][]);
System.out.println(bArray instanceof Object[]);
System.out.println(bArray.getClass().getSuperclass());
for (Class i: bArray.getClass().getInterfaces())
    System.out.println(i);
System.out.println(I[][].class.isAssignableFrom(bArray.getClass()));
System.out.println(I[][].class.isInstance(bArray));

Output:

输出:

true
true
true
class java.lang.Object
interface java.lang.Cloneable
interface java.io.Serializable
true
true

Furthermore, Java violates the Liskov substitution principle, because

此外,Java违反了Liskov替换原则,因为

B[] bArray = new B[1];
A[] aArray = bArray;
// bArray[0] = new A(); // causes a compile error
aArray[0] = new A(); // compiles, but causes runtime exception