It is not necessary to specify array size when creating array, right?
Then, why is arrayWithCapacity necessary?
And if I set the size of array smaller than actually needed, is it OK?
It is not necessary to specify array size when creating array, right? Then, why
Share
arrayWithCapacity is an optimization – it is not necessary. If you know the number of elements ahead of time, the system can allocate storage in one system call and in one chunk of memory. Otherwise, the system has to resize the array later as you add more elements and that tends to be slow, requiring additional allocations and possibly copying data from the old buffer to the new buffer.