Hi Freddy
What you say is right. Now, there are two interesting things to think
about when you conceive your code:
-- Programming facility (it is important to have a certain confort when
writing your code in order to advance quick enough - time is money)
-- Final program performances.
One of the earliest "collections" in Java was the Vector class, who is
about the same thing as an ArrayList.
Vector objects used to be very "user friendly" from the programmer point
of view, but too slow for time critical applications.
The reason of this, was the fact that the Vector operations are properly
protected in a multithreading environment. Container based operations
are not always performed in a multithreading context. So, in more recent
collections the concurential control was removed, leaving the programmer
either to perform his or her own thread synchronization (there is a
lesson about multithreading too) or use an already synchronized
collection class (such as the Vector). As both the "hardwired" thread
management and the "manual" synchronization are time consuming
operations, the "manual" solution may be better, as the programer may
perform it only on the relevant operations (from the program point of view).
In less words: Vector objects, LinkedList objects, ArrayList objects
offer all about the same facilities for the programmer, although their
internal implementation is different. The differences matter when the
performances are critical.
There is no "better" solution than another other as each implementation
perform quicker in a particular context (except for the Vector that is
almost always slower - however, the Vector seems very easy to use from
the programmer's point of view and spares a lot of time at least to the
beginner).
Hope it helps
Mihai
Le 06/01/2011 15:16, Freddy Castelblanco Macias a écrit :
Hi
The interface collection leaves to you do everithing that you want
using any kind of list . So you should read about them, perhaps it'll
helps to you. For remove any element of the list you can make it
saying to the list the index of your element. Or you can create a
function that delete the element that you want.
For add element the list or collection haVe two options or three isla
good watch the api of java.
And if the list or collection grow you can control that in the
constructor of the collection.
I like use the vector class
I hope help you. Luck and see you
2011/1/5, Mihai DINCA<mihai.di...@free.fr>:
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
--
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