数组是如何在java中实现的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2267790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How are arrays implemented in java?
提问by AFK
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.
数组在java中被实现为对象吗?如果是这样,我在哪里可以查看数组类的源代码。我想知道数组中的长度变量是否定义为常量,如果是这样,为什么它不是全部大写字母 LENGTH 以使代码更易于理解。
采纳答案by Yishai
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.
尽管从继承 java.lang.Object 的意义上讲,数组是对象,但类是作为语言的一个特殊功能动态创建的。它们没有在源代码中定义。
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().
长度为小写和字段的原因实际上是因为在开发该字段时不存在后来的 Java 编码标准。如果今天正在开发一个数组,它可能是一个方法: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.
长度是对象构造时定义的最终字段,它不是常量,因此一些编码标准不希望它是大写的。然而,在今天的 Java 中,一般来说,一切通常要么作为大写的常量完成,要么用公共 getter 方法标记为私有,即使它是最终的。
回答by Kaji Islam
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'....};