![]() We need delicate handling while using this constructor. If you choose initialCapacity too high, then the ArrayList would be too large and it would waste space. If we choose initialCapacity too low, then the ArrayList has to frequently create new arrays to accommodate more values. It may look like a good idea to use this constructor instead of public ArrayList() but we need to be careful. Once that is done, it copies all elements of the old array to the new array. When we add the 21st element, it will create a new array roughly 1.5 times of the old array size. So the underlying array has a capacity of 20 elements. In this example, we created the ArrayList with an initialCapacity of 20 elements and added two elements.
0 Comments
Leave a Reply. |