0

I understand from one of Array in Java posts that ArrayList is dynamic, whereas arrays are static in Java.

So if I want to modify the size of an array, it is not possible? Is there any workaround?

TechSavy
  • 11
  • 4

5 Answers5

1

A Java Array object is of fixed size. Inside ArrayList, there is an Array, which it also of fixed size, but as you add more items to your list, the ArrayList class automatically creates a larger internal Array and then copies the elements across to it.

If you want to have both flexible sizing AND array-like behaviour, then ArrayList is a good option. If you want to get a larger array sometimes, then you'll have to essentially do what ArrayList does: create a larger array and copy the existing elements across.

On the whole Array is useful for some situations where you want very efficient access to a fixed size set of data. The moment you think it might change size, you should consider on of the other, more flexible collection classes.

Ashley Frieze
  • 4,993
  • 2
  • 29
  • 23
0

Arrays having fixed lengths is how the language is defined to work.

Ultimately, all structures such as collections have a (fixed length) arrays under the covers, even the ArrayList, but its methods throw away the internal array and replace it with a larger one if it needs to grow beyond its current capacity.

Memory needs to be allocated, and for that you need to know how much, and for that you need to fix the size.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

In many modern languages, array is static and lists and/or vectors are dynamic since sometimes you need a static data structure and sometimes you need a dynamic data structure :)

There is a workaround, of course, but it is quite inefficient and meaningless since there are dynamic data structures you can always use.

You can create a new array of the next size and copy all data from the previous array to the new array. Then set the new array to the old array.

biarmic
  • 1
  • 2
  • 2
0

Arrays are statically sized in Java , even upon creation array size must be given (or the initial size).

Array of primitives will be initialised with the default value if this primitive across the size of the array, whileas array of Wrapper/Objects will be initialised with a null values across all of its elements.

Resizing an array in Java means manually creating a new array of the desired size and selectively adding elements to it from the old array.

(Even arraylist does resizing manually behind the scenes based on its metrics).

-1

ArrayList is can only store object datatype and is an object type. Arrays are reference types. Array does not have the capability to adjust its size however the ArrayList type has the capacity for that.

David Enoma
  • 311
  • 1
  • 13
  • Actually in Java arrays are reference types and _not_ primitive types. Also, to improve your answer, you might want to clarify what you mean by "calling data type" as there are no calls mentioned in the question. Cheers. – Ray Toal Feb 27 '21 at 10:03
  • I have made note of the corrections and edited my answer as so – David Enoma Feb 28 '21 at 15:22