ArrayList is better for adding and removing elements from the end of the container. All of the operations can be performed in constant time(i.e. time complexity) -- O(1).
Adding and removing elements from any other position is proven to be more expensive -- linear to be exact: O(n-i), where n refers the number of elements and i refers the index of the element to be added or removed.
For Example : ArrayList Has 6 elements and you want to add an element on 3rd position. Then ArrayList add the element on 3rd position and shifted existing 3rd position element to 4th, 4th position element to 5th, and so on.
LinkedList can add and remove an element at any position in constant time -- O(1). Indexing an element is a bit slower -- O(i) where i is the index of the element in case of LinkedList.
---------------------------------------------------------------------------------------------------------------------------------
Traversing ArrayList is also easier since you can simply use an index instead of having to create an iterator.
The LinkedList creates an internal object for each element inserted. So you have to be careful of the extra garbage being created.
---------------------------------------------------------------------------------------------------------------------------------
An ArrayList is a List implementation backed by a Java array. With a LinkedList, the List implementation is backed by a doubly linked list data structure.
---------------------------------------------------------------------------------------------------------------------------------
ArrayList stores objects in an array internally so it is fast in case of random access, and to insert a new element at a particular index it have to move subsequent elements one by one, hence it is slower while inserting and deleting.
LinkedList stores objects using nodes, each node has the address of next node, hence random access is slow and updation (i.e insertion and deletion) is faster in case of LinkedList.
5