InterviewSolution
| 1. |
How does the size of ArrayList grow dynamically? And also state how it is implemented internally. |
|
Answer» ArrayList is IMPLEMENTED in such a way that it can grow dynamically. We don't need to specify the size of ArrayList. For adding the values in it, the methodology it uses is - 1. Consider initially that there are 2 elements in the ArrayList. [2, 3]. 2. If we need to add the element into this. Then internally what will happen is-
3. This process continues and the time taken to perform all of these is considered as the amortized constant time. This is how the ArrayList grows dynamically. And when we DELETE any entry from the ArrayList then the FOLLOWING steps are PERFORMED - 1. It searches for the element index in the array. Searching takes some time. Typically it’s O(n) because it needs to search for the element in the entire array. 2. After searching the element, it needs to shift the element from the right side to fill the index. So this is how the elements are deleted from the ArrayList internally. Similarly, the search operations are also implemented internally as defined in removing elements from the list (searching for elements to delete). |
|