gpt4 book ai didi

java - 数组的长度属性在哪里定义?

转载 作者:太空宇宙 更新时间:2023-11-04 14:03:10 24 4
gpt4 key购买 nike

我们可以确定 ArrayList<E> 的长度使用其公共(public)方法size() ,比如

ArrayList<Integer> arr = new ArrayList(10);
int size = arr.size();

类似地我们可以确定 Array 的长度使用 length 的对象属性(property)

String[] str = new String[10];
int size = str.length;

size()方法ArrayList定义在 ArrayList 内类,这是哪里length Array的属性(property)定义?

最佳答案

数组是java中的特殊对象,它们有一个名为length的简单属性,它是final

数组没有“类定义”(您在任何 .class 文件中都找不到它),它们是语言本身的一部分。

10.7. Array Members

The members of an array type are all of the following:

  • 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.

  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method.
<小时/>

资源:

关于java - 数组的长度属性在哪里定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29114261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com