Komal Amaresh wrote:Sir,
why do we have to initialize the capacity of an ArrayList or reinitialise it later when collections are primarily used for dynamic assignment.
Thanking you,
with regards,
komal
(dont call me sir Please)
1. We do not have to initialise the ArrayList capacity at all its not mandatory.
2. From an optimizations Point of view:
lets suppose n=2 (array size increases by 2)
array : [][]
adding 1,2
array : [1][2]
adding 3
array : [1][2][3][] NEW ARRAY is Created
adding 4
array : [1][2][3][4]
adding 5
array : [1][2][3][4][5][] NEW ARRAY is Created
adding 6
array : [1][2][3][4][5][6]
lets suppose n=5 (array size increases by 5)
array : [][][][][]
adding 1,2
array : [1][2][][][]
adding 3
array : [1][2][3][][]
adding 4
array : [1][2][3][4][]
adding 5
array : [1][2][3][4][5]
adding 6
array : [1][2][3][4][5][][][][][] NEW ARRAY is created
what you would prefer in this case: n=2 or n=5 ?