Originally posted by Chetan Raju:
Which is faster for Iteration ? An ArrayList or a LinkedList ? What about insertions and deletions ?
Well about your question
LinkedList is a doubly linked list allowing constant time access to the first and last elements, constant time appending of a single element to the end of the list or prepending a single element to the beginning of the list (deletion is also quick from the front or back, and deletion near the front or back has low overhead), linear time random access to elements in the middle, and linear time iteration over all elements in order.
ArrayLists allow constant time random access to all elements, amortized constant time appending of a single element to the end of a list but gets sluggish if you want to add and delete stuff from places other than the end of the list, particularly the beginning.
LinkedLists will have a
higher memory overhead since even in the most efficient memory representation each element maintains two extra pointers, whereas an
ArrayList maintains an array no more than twice as large as contents
which won't cost more memory than maintaining a single extra pointer per element and will usually cost less. LinkedLists will also have an additional garbage collection overhead since a object is created for each element.
LinkedLists make handy queues and are also good when there won't be random access but there will be iteration with an Iterator or ListIterator along with calls to remove().
When you don't need to add or delete items in the middle or front of the list an ArrayList is more appropriate. Well Hope you have cleared your doubts now....
Well about the more or less comparasions check out following links
Final Performance Testing - Examples