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.
source share