问题描述
我们可以使用其公共方法size()
来确定ArrayList
的长度,例如
We can determine the length of an ArrayList<E>
using its public method size()
, like
ArrayList<Integer> arr = new ArrayList(10);
int size = arr.size();
类似地,我们可以使用 length
属性来确定 Array
对象的长度
Similarly we can determine the length of an Array
object using the length
property
String[] str = new String[10];
int size = str.length;
而ArrayList
的size()
方法是在ArrayList
类中定义的,这个length
在哪里Array
的属性定义了吗?
Whereas the size()
method of ArrayList
is defined inside the ArrayList
class, where is this length
property of Array
defined?
推荐答案
数组是 java 中的特殊对象,它们有一个名为 length
的简单属性,即 final
.
Arrays are special objects in java, they have a simple attribute named length
which is final
.
数组没有类定义"(在任何 .class 文件中都找不到),它们是语言本身的一部分.
There is no "class definition" of an array (you can't find it in any .class file), they're a part of the language itself.
数组类型的成员如下:
public
final
字段length
,其中包含数组的组件数.length
可以是正数或零.public
方法clone
,覆盖Object
类中的同名方法,不抛出任何检查异常.数组类型T[]
的clone
方法的返回类型为T[]
.
- The
public
final
fieldlength
, which contains the number of components of the array.length
may be positive or zero. The
public
methodclone
, which overrides the method of the same name in classObject
and throws no checked exceptions. The return type of theclone
method of an array typeT[]
isT[]
.
多维数组的克隆是浅的,也就是说它只创建了一个新数组.子数组是共享的.
A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.
资源:
这篇关于数组的长度属性在哪里定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!