Why is array length immutable?

Why is the length of the array immutable (at least in java)? I know almost nothing about the internal workings of the programming language, so it seems to me that it is easy to change the length of the array. I assume there is a good reason that the lengths of arrays are immutable, probably related to performance. Does anyone know this reason?

Sorry if this question has been asked before. If so, I did not find it after about 10 minutes of searching.

EDIT: I know that when initializing an array, a certain amount of memory is allocated. Why is no more memory allocated?

+6
source share
8 answers

The essence of this problem is how the language developers decided to separate the tasks that the language should perform and assign them to various constructs.

In Java, the standard type of array has a fixed length, because it is designed to be implemented at the low level, barebones, which the developer can create on top.

In contrast, if you want to use a more fully functional array, check out the ArrayList<> class. The idea is to provide you, the developer, with a wide range of options for your tools, so you can always choose something suitable for the job.

Speaking of arrays, the technique needed for variable-length arrays is non-trivial. Therefore, the creators of Java decided to include this functionality in the ArrayList<> class, instead of making it mandatory overhead for everyone who just wants a simple array.

Why is array lengthening nontrivial? Computer memory is finite, so we must store objects next to each other. When you want to grow your array, what if there is another object in the neighboring memory?

In this case, there is not enough space to lengthen the array in place, so you need to move the data to free up space. This is usually done by finding a larger, empty block of memory and copying the array when it is extended. The mathematics of how to do this in the best way (for example, how much empty space does it take?) Are interesting and non-trivial. ArrayList<> annotates this process for you, so you do not need to process it yourself, and you can be sure that the developers of the ArrayList<> class have chosen an implementation that will work on average for your application.

+9
source

Nothing to do with performance. Length is the size that has been highlighted. You cannot change what you already installed when you created it. Size is the amount of space allocated, and you also cannot change it.

What you can do is take a copy of the array with a different size.

+5
source

As stated in the Oracle Java Array Tutorial :

An array is a container object that contains a fixed number of values ​​of the same type. The length of the array is set when the array is created. After creation, its length is fixed.

The length is fixed due to the structure itself. When you define one, you specify a certain number of elements for which the amount of memory should be allocated.

If you ever have to “resize” an array, you must create an array with a larger capacity and copy the existing elements onto it. There are many ways to do this.


Edit

When creating an array, a certain amount of memory is allocated, sufficient to store the number of required instances (values ​​for primitives, object references). Resizing an array implies expanding this memory block, and there is no guarantee that it will be contiguous.

+4
source

An array type can be implemented in such a way as to provide any two of the following:

  • Ability to resize array and links to old array become links to new

  • The ability for multiple threads to control the array simultaneously and independently, without blocking or other such measures, provided that neither of the two threads is trying to write the same part of the array and that any stream that reads part of the array that was written in another stream will work correctly with old or new data.

  • Access to an array element using a one-time, not double or triple direction.

While array types could be designed so that references to an array of a given size became references to an array of a different size, arrays would lose most of the thread safety. Of course, problems would only arise if the data in the array was recorded during the modification of the array, but there is no good way to protect this feature without using locks to protect all records in the array, which significantly increases their cost.

Alternatively, you could have an array wrapper containing a reference to arrays of links to elements, instead of holding elements directly, in which case the array could be expanded by taking a lock, copying all links to elements from the old array to a larger one, selecting new objects for storing new elements and placing links to them in a new array, updating the array reference so that it points to a new array and releases the lock. Locking is only necessary to protect against simultaneous attempts to resize. Reading and writing elements can be performed just fine, without blocking, since each element that exists in both old and new arrays will be guaranteed to be the same in both (i.e. both will contain references to the same object, and records arrays will not change the links stored in any of the arrays, but instead will modify the object identified by these links]. Such an approach could avoid the need for any blocking when making any changes other than resizing the array, but, unfortunately, accessing any element of the array will take too much work.

When designing the array types for .NET and Java, the developers decided that the first line above was the least important and thus aimed at providing the other two.

+2
source

Some programming languages ​​do allow this, for example, the VB.net redim function. With Java, C / C ++, etc. It can be attributed to other data structures, such as vectors and linked lists.

To add to this, when elements are stored in an array, they are stored contiguously (one after the other), so to resize it, what is done behind the scenes for languages ​​that support it out of the box is that the new array is actually created from a new size , and the content is dumped into it (in the case of VB.net, you must explicitly tell it to copy the original content, as well as iirc). The real problem with resizing arrays on the fly is that you need to know in advance how large the array can be, so an appropriate amount of contiguous memory can be allocated to store all the elements.

+1
source

This is a design choice. This is less about how Java works internally and more about giving the programmer more freedom to use only the resources they need.

A variable-length collection, such as an ArrayList , contains several methods, pointers, and overhead. If your data does not change size in the near future, why do you need overhead?

+1
source

Java supports both fixed-length arrays and ArrayList objects, the size of which can change. As others have indicated, the latter involves additional overhead; if a certain size is allocated for the array, and you later want to increase it, then in the general case, either the code should allocate new space for the larger array, or move it all, or the ArrayList should be implemented using links that can slow down, or, perhaps some combination of the two. So this is a compromise, and you can choose the one that best suits your needs. I think that the languages ​​that I know about provide only the second type of array (Perl, JavaScript, PHP), which, it seems to me, are interpreted languages, which means that running programs in this language will be essentially slower; this may be the reason that the developers of these languages ​​felt that performance was not so important.

+1
source

An array in almost any language has a fixed size. This is related to memory allocation: when you tell Java you want an array of size N, it exits and allocates a continuous block of memory. Size is determined by N * (the size of an individual item). The reason that the size cannot be resized is because there is no guarantee that before or after the array is in memory. To add another element, you need to “require” space in memory immediately after your array. However, this can already be used by others. To avoid complications and overhead, they simply made it so that you cannot resize the array.

What you can do is select a new larger array and copy all the old elements into it. Here's how data structures, such as ArrayList, work under the hood.

Hope that helps

+1
source

Source: https://habr.com/ru/post/958160/


All Articles