java中数组是如何实现的?

发布于 2024-08-21 04:15:17 字数 106 浏览 4 评论 0原文

数组在java中是作为对象实现的,对吗?如果是这样,我在哪里可以查看数组类的源代码。我想知道数组中的长度变量是否被定义为常量,如果是的话,为什么它不是全部大写字母 LENGTH 以使代码更容易理解。

Arrays are implemented as objects in java right? If so, where could I look at the source code for the array class. I am wondering if the length variable in arrays is defined as a constant and if so why it isn't in all capital letters LENGTH to make the code more understandable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

懒猫 2024-08-28 04:15:17

尽管数组在继承 java.lang.Object 的意义上是对象,但这些类是作为该语言的一个特殊功能动态创建的。它们没有在源代码中定义。

考虑这个数组:

MySpecialCustomObject[] array;

没有这样的源代码。您已在代码中动态创建了它。

长度之所以是小写且是一个字段,实际上是因为在开发它时,后来的 Java 编码标准还不存在。如果今天开发一个数组,它可能是一个方法:getLength()。

长度是在对象构造时定义的最终字段,它不是常量,因此某些编码标准不希望其为大写。然而,一般来说,今天的 Java 中的所有内容通常要么作为大写常量完成,要么使用公共 getter 方法标记为私有,即使它是最终的。

Although arrays are Objects in the sense that they inherit java.lang.Object, the classes are created dynamically as a special feature of the language. They are not defined in source code.

Consider this array:

MySpecialCustomObject[] array;

There is no such source code for that. You have created it in code dynamically.

The reason why length is in lower case and a field is really about the fact that the later Java coding standards didn't exist at the time this was developed. If an array was being developed today, it would probably be a method: getLength().

Length is a final field defined at object construction, it isn't a constant, so some coding standards would not want that to be in upper case. However in general in Java today everything is generally either done as a constant in upper case or marked private with a public getter method, even if it is final.

坚持沉默 2024-08-28 04:15:17

对于我们声明的每个数组,Java 中都有相应的类,但我们无法使用它。您可以使用 getClass().getName() 查看类

    int[] arr=new int[10];
    System.out.println(arr.getClass().getName());

输出: [I

where "[ ”代表一维数组,“I”代表整数。
同样,我们可以有

    [F for one-dimensional float arrays
    [Z for one-dimensional boolean arrays
    [J for one-dimensional long arrays
    [[I for two-dimensional int arrays

等等。

For every Array we declare, corresponding classes are there in Java but it's not available to us.You can see the classes by using getClass().getName()

    int[] arr=new int[10];
    System.out.println(arr.getClass().getName());

Output : [I

where "[" represents one dimension array and "I" represents Integer.
Similarly, we can have

    [F for one-dimensional float arrays
    [Z for one-dimensional boolean arrays
    [J for one-dimensional long arrays
    [[I for two-dimensional int arrays

and so on.

反话 2024-08-28 04:15:17

在Java中实现数组需要访问内存位置或进行指针运算。由于 Java 不允许您分配内存,因此它会为您执行数组实现。 Java 语言提供了这种实现。

Implementing array in Java requires access to memory location or do pointer arithmetic. As Java does not let you to allocate memory, it does the Arrays implementation for you. Java language provides that implementation.

尹雨沫 2024-08-28 04:15:17

我们可以说数组是保存固定长度的单一数据类型数据的容器。
例如。

int[] MyArray = new int[101]; // allocates memory for 101 integers, Range from 0 to 100.

对于多维

String[][] names = {{"FirstName", "LastName"},{"Kaji", "Islam"},...};

和字符数组

char[] ch={'a','b'....};

We can say that An array is a container that holds a fixed length of data of single data type.
eg.

int[] MyArray = new int[101]; // allocates memory for 101 integers, Range from 0 to 100.

and for multidimensional

String[][] names = {{"FirstName", "LastName"},{"Kaji", "Islam"},...};

and for character array

char[] ch={'a','b'....};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文