我正在阅读 API对于 ArrayType
类,它是 ASTNode
的子类。
有一个方法 getComponentType()
和一个方法 getElementType()
。
对于语句
:int[] list = new int[50]
,上述两个方法都返回类型 - int
。
有人知道这两种方法之间的区别吗?
最佳答案
Javadoc 有一个包含简短描述的表格,但您必须向下滚动才能阅读完整的说明。对于 getComponentType()
,它表示:
Returns the component type of this array type. The component type may be another array type.
而对于getElementType()
,它表示:
Returns the element type of this array type. The element type is never an array type.
This is a convenience method that descends a chain of nested array types until it reaches a non-array type.
例如,int[][]
的“组件类型”为 int[]
,但“元素类型”为 int
.
关于Java数组: Difference between Component and Element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17033454/