数组的长度属性在哪里定义? [英] Where is array's length property defined?

查看:30
本文介绍了数组的长度属性在哪里定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用其公共方法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;

ArrayListsize()方法是在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 field length, which contains the number of components of the array. length may be positive or zero.
  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].

多维数组的克隆是浅的,也就是说它只创建了一个新数组.子数组是共享的.

A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.

<小时>

资源:

这篇关于数组的长度属性在哪里定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆