ArrayList vs LinkedList - Java @ Desk

Thursday, May 16, 2013

ArrayList vs LinkedList

1) Insertion and deletion is faster in LinkedList compared to ArrayList.
2) Iteration is quicker in ArrayList

Illustration to show how insertion is faster in LinkedList

ArrayList arrayList = new ArrayList();
arrayList.add(10);
arrayList.add(20);
arrayList.add(30);
arrayList.add(40);
arrayList - > 10--20--30--40

Insert 50 at position 2 i.e. after 20 involves below mentioned operations
arrayList.add(1, 50);
a) Shift value ’30’ at node 3
b) Shift value ‘40’ at node 4
c) Insert value ‘50’ at node 2

Resulting arraylist is 10--50--20--30--40


LinkedList linkedList = new LinkedList ();
linkedList.add(10);
linkedList.add(20);
linkedList.add(30);
linkedList.add(40);

linkedList - > 10--20--30--40

Insert 50 at position 2 i.e. after 20 involves below mentioned operations

a) Point value ’50’ upper node to value ‘10’ and lower node to value ‘20’

10--50--20--30--40






No comments:

Post a Comment