简体   繁体   English

数组长度

[英]Length in Arrays

Just a simple question, but why is length in an array not encapsulated? 只是一个简单的问题,但为什么数组中的length没有封装? Is this just for beginners in programming because I feel like a getLength() method would be easy to implement. 这是否适合初学者编程,因为我觉得getLength()方法很容易实现。

I've an assumption but I cant say that this is the reason why the designers of java did it that way. 我有一个假设,但我不能说这就是为什么java的设计师这样做的原因。

An array is a container (and an Object of course) with a fixed length. 数组是具有固定长度的容器(当然是对象)。 And its a very basic one, so encapsulation in this case would be nice but not critical. 它是一个非常基本的,所以在这种情况下封装会很好但不重要。 Take a look at ArrayList for example, which can be considered as an encapsulation of an array. 例如,看看ArrayList,它可以被视为数组的封装。

A basic array has many benefits over other types that encapsulates it. 与封装它的其他类型相比,基本数组有许多优点。 And so maybe when the first version of Java came out it lacked most of the optimization that it has today. 因此,当第一版Java出现时,它缺乏今天的大部分优化。 So having a data structure with the simplicity (in term of performance) of an array is a good thing. 因此,拥有一个具有数组简单性(在性能方面)的数据结构是一件好事。

Also it could be as simple as they didnt think about that at that time and afterwards they had to maintain a backwards compatibility :) 它也可能就像他们当时没有想到那样简单,之后他们必须保持向后兼容性:)

Anyway, if you find interesting stuff about that (maybe you could email sun/oracle) please post! 无论如何,如果你发现有趣的东西(也许你可以发送电子邮件太阳/甲骨文),请发布! Thats a nice question. 这是一个很好的问题。

Although arrays might be interpreted as objects, they're really just a fixed set of memory addresses containing some other object or variable. 虽然数组可能被解释为对象,但它们实际上只是包含一些其他对象或变量的固定内存地址集。 In this manner, they have attributes, such as .length , but not necessarily methods, like .getLength() 以这种方式,它们具有属性,例如.length ,但不一定是方法,如.getLength()

A lot of information about arrays can be found here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html 有关数组的大量信息可以在这里找到: http//docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

That would be easy to implement, and that's why it isn't implemented. 这很容易实现,这就是为什么它没有实现。

Arrays are simply blocks of storage, with no real controls. 数组只是存储块,没有真正的控件。 They enable an easy and fast storage method for when advanced controls are not required. 它们可以在不需要高级控制时实现简单快速的存储方法。

You can use things like lists, queues, linked lists, double linked lists etc. which can keep a record of their size, if this is required. 如果需要,您可以使用列表,队列,链接列表,双链表等等可以记录其大小的内容。 Obviously, depending on the language you are using, you may well have to create these yourself. 显然,根据您使用的语言,您可能必须自己创建这些语言。

If you want to have length of array and still use a simple array, you can add 1 to a count variable every time you add something to it and minus one each time you remove something. 如果你想拥有数组的长度并仍然使用一个简单的数组,你可以在每次向其添加内容时向count变量添加1,在每次删除内容时减去1。 This will perform the same function as a getLength() method. 这将执行与getLength()方法相同的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM