I need an array that I can determine its size from the start and if it has no more empty spaces then increase its size by X
. For example:
int arrayS
I think ArrayList
is better in this case instead of simple arrays because automatically grows. From the javadoc:
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
But if you have to use arrays, Arrays utility class comes to your aid
intArray = Arrays.copyOf(intArray, intArray.length + x);