为什么是一个数组的长度不变? [英] Why is an array's length immutable?

查看:405
本文介绍了为什么是一个数组的长度不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么是一个数组的长度不变(至少在Java)?我知道一门编程语言的内部运作微乎其微,所以对我来说似乎很容易使一个数组的长度改变。我认为有一个很好的理由,数组的长度是不可变的,可能与性能。有谁知道这个道理?

Why is an array's length immutable (at least in java)? I know next to nothing about the inner workings of a programming language, so to me it seems easy to make an array's length changeable. I assume that there is a good reason for array lengths being immutable, probably related to performance. Does anybody know this reason?

很抱歉,如果这个问题已经被问了。如果是,我并没有发现它在大约10分钟的寻找。

Sorry if this question has been asked before. If it has, I didn't find it in about 10 minutes of searching.

编辑:我知道,当一个数组初始化的内存一定量的分配。为什么不能更多的内存分配?

I know that when an array is is initialized, a certain amount of memory is allocated. Why can't more memory be allocated?

推荐答案

这个问题的本质在于语言的设计者会选择如何划分了一个语言需要执行的任务,并将其分配给不同的结构。

The essence of this problem lies in how language designers chose to partition up the tasks that a language needs to perform, and assign those to different constructs.

在Java中,标准阵列类型具有固定的长度,因为它的意图是一个低的水平,准系统实现中,开发人员可以建立在顶

In Java, the standard array type has a fixed length, because it's intended to be a low-level, bare-bones implementation, that the developer can build on top of.

在相反,如果你想有一个更全面的功能阵列,请查看的ArrayList<> 类。我们的想法是给你的开发人员,广泛的你的工具的选择,所以你可以总是挑适合这份工作什么的。

In contrast, if you want a more fully featured array, check out the ArrayList<> class. The idea is to give you, the developer, a wide range of choices for your tools, so you can always pick something appropriate for the job.

在谈到阵列,有必要允许变长数组的机械是平凡的。所以Java的创造者选择了把这个功能在的ArrayList&LT;&GT; 类,而不是使之成为法定管理的人谁只是想要一个简单的数组

When talking about arrays, the machinery necessary to allow for variable-length arrays is nontrivial. So the creators of Java chose to put this functionality in the ArrayList<> class, rather than making it mandatory overhead for anyone who just wants a simple array.

为什么延长一个数组平凡?计算机内存是有限的,所以我们要存储相邻的对象。当你想延长你的阵列,如果有就是已经在附近的内存地址另一个对象?

Why is lengthening an array nontrivial? Computer memory is finite, so we have to store objects next to each other. When you want to lengthen your array, what if there's already another object in the nearby memory addresses?

在这种情况下,没有足够的空间来延长就地阵列,因此人们必须到处移动数据以腾出空间。通常,这是通过找到的内存较大,空块,并复制到阵列,同时延长过来做。如何做到这一点的最好的方式数学(例如多少空留?)是有趣的,平凡的。 的ArrayList&LT;&GT; 抽象这个过程你,所以你不必自己处理它,你可以确信的设计师 ArrayList的&LT;&GT; 类选择了将表现良好(平均)实现为您的应用

In this case, there isn't enough space to lengthen the array in-place, so one has to move data around to make room. Usually this is done by finding an larger, empty block of memory, and copying the array over while lengthening it. The mathematics of how to do this in the best way possible (e.g. how much empty space to leave?) are interesting and nontrivial. ArrayList<> abstracts this process for you, so you don't have to handle it yourself, and you can be confident that the designers of the ArrayList<> class chose an implementation that will perform well (on average) for your application.

这篇关于为什么是一个数组的长度不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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