Linklist and ArrayList
Today i was turning back towards Linklist and a question dropped in my mind is when to use Linklist and When to use ArrayList.
They both are different in implementations. LinkedList implements it with a doubly-linked list. ArrayList implements it with a dynamically resizing array. If you take up Linklist you can walk the lists forward or backwards, but grabbing an element in the middle takes time which depeneds upon your size of the list.
ArrayLists allows you random access, but adding and removing from anywhere smells your time which requires shifting all the latter elements. When you add more elements more than the capacity, a new array twice of the default size is allocated and the older array is copied to newer one.
When should i use LinkedList?
- When you need efficient removal in between elements or at the start.
- When you don’t need random access to elements, but can live with iterating over them one by one
When should i use ArrayList?
- When you need random access to elements (“get the nth. element”)
- When you don’t need to remove elements from between others. It’s possible but it’s slower since the internal backing-up array needs to be reallocated.
- Adding elements is amortized constant time (meaning every once in a while, you pay some performance, but overall adding is instantly done)

Recent Comments