Thursday, December 6, 2007

Java LinkedList vs ArrayList

Which one is better ArrayList or LinkedList ?

An ArrayList is a List implementation backed by a Java array, similar to the Vector class. As the number of elements in the collection increases, the internal array grows to fit them. If there are lots of growth periods, performance degrades as the old array needs to be copied into the new array. However, random access is very quick as it uses an array index to access.

With a LinkedList, the List implementation is backed by a doubly linked list data structure, allowing easy inserts/deletions anywhere in the structure, but really slow random accesses as the access must start at an end to get to the specific position.

Which one should you use really depends on the type of operations you need to support.

Source: http://www.jguru.com/faq/view.jsp?EID=106663

No comments: