简体   繁体   English

Java - 是否可以子类化数组?关于Java中的数组的更多问题

[英]Java - Is it possible to subclass an array? And more questions about arrays in Java

These questions are purely asked out of curiosity. 这些问题纯粹是出于好奇而被问到的。 I don't actually need to subclass an array, I'm just trying to figure out more about how they work in Java. 我实际上并不需要子类化数组,我只是想弄清楚它们如何在Java中工作。

  1. Where is the Javadoc API for arrays? 阵列的Javadoc API在哪里? I found one for the 'Arrays' class, but that class just contains utilities to use on Java arrays, and is not the actual array class. 我找到了一个'Arrays'类,但该类只包含在Java数组上使用的实用程序,而不是实际的数组类。 This leads me to my next question: 这引出了我的下一个问题:

  2. IS there an actual array class of which all arrays are subclasses? 是否有一个实际的数组类,其中所有数组都是子类?

  3. Is Object[] a superclass of String[] (for example)? Object[]的类的超类String[]例如)? I'm guessing the answer here is no. 我猜这里的答案是否定的。 Are these actual classes like any other class? 这些实际的课程和其他课程一样吗?

  4. Is String[] a different class from String[][] ? String[]是一个与String[][]不同的类吗? Or String[][][] , etc? 还是String[][][]等?

  5. As asked in the title, is it possible to subclass an array class (or to subclass THE array class? still not sure how it works as you can tell by my above questioning)? 正如标题中所要求的那样,是否有可能对数组类进行子类化(或者对数组类进行子类化?仍然不确定它是如何工作的,因为你可以通过上面的问题来判断)? Could I create my own class, instances of which acted exactly like arrays, except they had more functionality? 我可以创建自己的类,其实例与数组完全相同,除非它们具有更多功能吗?

Thanks. 谢谢。

The Java Language Specification answers all these questions: Java语言规范回答了所有这些问题:

The direct superclass of an array type is Object. 数组类型的直接超类是Object。 Every array type implements the interfaces Cloneable and java.io.Serializable. 每个数组类型都实现了Cloneable和java.io.Serializable接口。

So no, there isn't a common base class for all arrays, and therefore no JavaDoc either. 所以不,所有数组都没有通用的基类,因此也没有JavaDoc。 The members of arrays are defined by the spec instead. 数组的成员由规范定义

Subtyping among array types is defined in section 4.10.3 - and yes, String[] is a subtype of Object[] . 数组类型之间的子类型在4.10.3节中定义 - 是的, String[]Object[]的子类型。 See also ArrayStoreException . 另请参见ArrayStoreException

Yes, String[].class != String[][].class . 是的, String[].class != String[][].class (cf section 10.8 ) (参见第10.8节

No, there is no syntax to subclass arrays. 不,没有语法来子类化数组。 In particular, the extends clause must contain a class type (and array types are not class types). 特别是,extends子句必须包含类类型(并且数组类型不是类类型)。

There is no array class in Java. Java中没有数组类。

Interestingly, arrays are objects (but not class instances): 有趣的是, 数组是对象 (但不是类实例):

An object is a class instance or an array. 对象是类实例或数组。

More here: http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html 更多信息: http//docs.oracle.com/javase/specs/jls/se7/html/jls-10.html

The classes implementing java.util.List provide a more object-oriented implementation of array-like structures. 实现java.util.List的类提供了更像面向数组的结构的面向对象的实现。

You can't subclass arrays. 您不能子类化数组。 Even though the syntax used with them is a bit different, they are objects (check with the JLS ). 即使与它们一起使用的语法有点不同,它们也是对象(使用JLS检查)。 There's not much API to them - apart from just what Object has (with toString() not doing what you expect, use Arrays.deepToString() for that; equals() and hashCode() are similar) there's the length field. 它们没有太多的API - 除了Object之外( toString()没有做你期望的,使用Arrays.deepToString() ; equals()hashCode()是相似的)有length字段。 Additionally, arrays are cloneable. 此外,阵列是可复制的。 You can only cast array types if the target element type is a supertype of the source element type - you can cast String[] to Object[] but not the other way around. 如果目标元素类型是源元素类型的超类型,则只能转换数组类型 - 您可以将String[]Object[]但不能反过来。 If you are sure the objects in the array are a specific type, you can cast each element individually. 如果您确定数组中的对象是特定类型,则可以单独转换每个元素。 String[][] is an array of String[] , so it's a different type than String[] as its elements are arrays of String, not Strings. String[][]是一个数组String[]所以它是一个不同的类型String[]作为其元素是串的阵列,而不是字符串。 You can create classes which give similar functionality to arrays ( ArrayList does just that), but they will not be interchangeable with regular arrays. 您可以创建为数组提供类似功能的类( ArrayList就是这样),但它们不能与常规数组互换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM