The question is which one to use and when?
Array - This is basically a set of elements of a fixed size. The bad point about the array is that it does not change. But its constant size provides efficiency if you are free from the size of your element. Therefore, arrays are best used when you know the number of elements available.
Collection
ArrayList is another set in which the number of elements can be changed. Therefore, if you are not sure about the number of elements in a collection, use an ArrayList. But when using ArrayLists, some facts need to be considered.
ArrayLists are not syncing. So if there are several access streams and list changes, then synchronization may be required for processing externally.
ArrayList is internally implemented as an array. Therefore, whenever an array of n + 1 elements is added to a new element, then all n elements are copied from the old array to the new array, and then the new element is inserted into the new array.
Adding n items is required on time.
IsEmpty, size, iterator, set, get, and listIterator operations
require the same amount of time, regardless of the item you are accessing.
Only objects can be added to ArrayList
Allows null elements
If you need to add a large number of elements to an ArrayList, you can use the health operation (int minCapacity) to ensure that the ArrayList has the required capacity. This ensures that the array will be copied only once, when all elements are added and will increase the performance of adding elements to the ArrayList. In addition, inserting an element in the middle of 1000 elements will require you to move 500 elements up or down, and then add the element in the middle.
The advantage of using ArrayList is that access to random elements is cheap and does not depend on the number of elements in ArrayList. But adding elements to the head of the tail or in the middle is expensive.
A vector is similar to an ArrayList with the difference that it is synchronized. It offers some other benefits, such as initial capacity and additional features. Therefore, if your vector has a capacity of 10 and incremental capacity of 10, then adding the 11th element will create a new vector with 20 elements, and 11 elements will be copied to the new vector. Thus, adding 12-20 elements will not require the creation of a new vector.
By default, when a vector needs to increase the size of its internal data structure to store more elements, the size of the internal data structure doubles, while for ArrayList, the size increases by only 50%. Therefore, an ArrayList is more conservative in terms of space.
LinkedList is much more flexible and allows you to insert, add and remove elements on both sides of your collection - it can be used as a queue or even a double queue! Internally, LinkedList does not use arrays. LinkedList is a sequence of nodes that are double. Each node contains a header where the objects are actually stored, and two links or pointers to the next or previous node. LinkedList looks like a chain made up of people who hold each other's hands. You can insert people or node into this chain or delete. Linked lists allow node to insert / delete an operation at any point in the list at a constant time.
Thus, the insertion of elements into a linked list (whether on the head or in the tail or in the middle) is small. Also, when you extract items from your head, it is cheap. But if you want to accidentally access the elements of a linked list or access the elements at the tail of the list, then the operations will be difficult. The reason is, to access the n + 1th element, you need to analyze the first n elements in order to reach the n + 1th element.
Also the linked list is not synchronized. Therefore, several threads modifying and reading the list must be synchronized from the outside.
Therefore, the choice of which class to use for creating lists depends on the requirements. ArrayList or Vector (if you need synchronization) can be used when you need to add elements at the end of the list and access elements at random - more access operations than add operations. While LinkedList should be used when you need to do a lot of add / remove operations (items) from the head or in the middle of the list, and your access operations are relatively smaller.