Hi Eliscinsky
The ArrayList is implemented using an array:
-- If you add more elements than the array size, then a new bigger array
is (probably) created and the elements are copied from the old into the
new one
-- When you insert an element at any position except at the end and when
you delete an element at any position except the end, then the remaining
elements (probably) must be moved one by one
-- En exchange to the above time consuming operations, you can read the
value of the "n"th element directly (direct access)
The LinkedList is implemented as a double linked list:
-- If you need the "n"th element in the list, then all the elements
(probably) from the closest end and up to the "n"th element must be read
-- But you can easily insert or supress elements at any position
In brief:
-- If you need to access only the first or the last element of the list
(such as when implementing a queue or a heap) or if your need a storage
for elements that are oftenly added/removed, then use a LinkedList
-- If you need a storage for a predictable number of elements that
almost never change but you need a quick direct access to the elements,
then use an ArrayList
-- If you don't know which is better, then use the List interface and
specify the list implementation only in the constructor (such as
List myList = new ArrayList();
). Run once with an ArrayList and once with a LinkedList, compare
performances then choose the better.
Hope it helps
Mihai
Le 05/01/2011 19:53, eliscinsky a écrit :
In Exercise 2, second paragraph states ...
There are two general-purpose List implementations in the Collections
Framework: ArrayList and LinkedList. Which of the two List
implementations you use depends on your specific needs. If you need to
support random access, with inserting or removing elements from any
place other than the end, then ArrayList offers the optimal
collection. If, however, you need to frequently add and remove
elements from the middle of the list and only access the list elements
sequentially then LinkedList offers the better implementation.
My confusion is this ...
A) If ... inserting or removing ... any place other than the end, then
ArrayList offers the optimal collection.
"any place other than the end" - This means adding and removing from
the middle of the List. Correct?
B) If, however, ... add and remove elements from the middle ... then
LinkedList offers the better implementation.
"add and remove elements from the middle" - This also means adding and
removing from the middle of the List. Correct?
But as stated from JavaDoc of the LinkedList (http://
download.oracle.com/javase/1.5.0/docs/api/index.html?java/util/
ArrayList.html), which notes "remove and insert an element at the
beginning and end of the list"
Linked list implementation of the List interface. Implements all
optional list operations, and permits all elements (including null).
In addition to implementing the List interface, the LinkedList class
provides uniformly named methods to get, remove and insert an element
at the beginning and end of the list. These operations allow linked
lists to be used as a stack, queue, or double-ended queue (deque).
Please advise.
--
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en