InterviewSolution
| 1. |
Why Arraylist Is Called Dynamically Growing Array? How That Dynamic Behaviour Is Achieved? |
|
Answer» In the case of ArrayList you don't have to anticipate in advance, like in the case of array, how many ELEMENTS you are going to STORE in the arraylist. As and when elements are added list keeps GROWING. That is why ArrayList is called dynamically growing array. For ArrayList, data structure used for storing elements is array itself. When ArrayList is created it initializes an array with an INITIAL capacity (default is array of length 10). When that limit is crossed another array is created which is 1.5 TIMES the original array and the elements from the old array are copied to the new array. In the case of ArrayList you don't have to anticipate in advance, like in the case of array, how many elements you are going to store in the arraylist. As and when elements are added list keeps growing. That is why ArrayList is called dynamically growing array. For ArrayList, data structure used for storing elements is array itself. When ArrayList is created it initializes an array with an initial capacity (default is array of length 10). When that limit is crossed another array is created which is 1.5 times the original array and the elements from the old array are copied to the new array. |
|