In: Computer Science
What is an array-based list?
What is a resizable list?
What is the difference between a list’s capacity and its size?
When a list is expanded, is the size changed or is its capacity
changed?
Answer:-
What is an array-based list?
The array-based list, also known as array list, is a resizable array implementation. Thus, as more elements are added to the linked list, its size increases dynamically. The array-based list assigns an element to the assigned array; however, if a new element is assigned some data, and there is no space in the array, then it allocates a new array, and moves all the data to the newly allocated array
What is a resizable list?
Resizable array" means that the size of the array can dynamically increase or decrease when you are adding or deleting elements to/from the array(in this case ArrayList). As opposed to the primitive arrays, ArrayList don't require you to specify the size of the data structure while creating an ArrayList object i.e. the ArrayList grows/shrink as you start adding/removing elements from the ArrayList.
For example
The above line is an example of primitive array in which you have to specify the size of the array(in this case, 5) which means you can't resize the array.
The above line is an example of an ArrayList object of type Integer. We haven't defined the size of the ArrayList while creating it, the ArrayList takes care of increasing/decreasing(resizing) the data structure and hence it is an resizable-array implementation.
What is the difference between a list’s capacity and its size?
The size of the list is the number of elements in it. The capacity of the list is the number of elements the backing data structure can hold at this time. The size will change as elements are added to or removed from the list. The capacity will change when the implementation of the list you're using needs it to. (The size, of course, will never be bigger than the capacity.)
(BTW, for fixed size containers the size is frequently called length, thus arrays have a property length and strings have a method length(). Different languages - sometimes even the same language - use "size" and "length" inconsistently, but they always mean size, and the term "capacity" is always used for the size/length of the underlying data structure.)
When a list is expanded, is the size changed or is its capacity changed?
capacity changed
why:-
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.
Also, for an ArrayList, size can not be set while initializing. However the initial capacity can be set. Size is the number of elements in the list