Array vs arraylist vs
linkedlist vs Vector with very good overview and examples. An ArrayList
is ordered collection (also known as a sequence). It means that the user
of controls where each element is inserted. This class is very similar
to the Vector class, excepting that it is not synchronized. It must be
synchronized externally.
An ArrayList is better than Array to use when you have no knowledge in advance about elements number. ArrayList are slower than Arrays. So, if you need efficiency try to use Arrays if possible.
Short Overview on main features of ArrayList:
- The add operation runs O(n) time. - The isEmpty, size, iterator, set, get and listIterator operations require the same amount of time, independently of element you access. - Only Objects can be added to an ArrayList - Implements all methods from the List interface - Permits null elements An arraylist could be a good choice if you need very flexible Array type of collection with limited functionality.
One small remark for those who needs faster arraylist. arraylists internally implemented as an Array. So when run an "add" command a new Array will be created with n+1 dimension. All "older" elements will be copied to first n elements and last n+1 one will filled with the value which you provide with add() method. To avoid that internal re-copying of Arrays you should use ensureCapacity(int requestCapacity) method - it will create an array only once.
If you have higher requirements on number of accessible methods for your Array like collection instead of arraylist you have better choice - linkedlist.
linkedlist is much more flexible and lets you insert, add and remove elements from both sides of your collection - it can be used as queue and even double-ended queue!
Internally a linkedlist does not use arrays, it is much more modern! linkedlist is a sequence of nodes, which are double linked. Each node contains header, where actually objects are stored, and two links or pointers to next or previous node. A linkedlist looks like a chain, consisting of people who hold each other's hand. You can insert people or node into that chain or remove. Linked lists permit node insert/remove operation at any point in the list in constant time.
linkedlist is actually luxury extension of an arraylist: it gives you many methods which you usually implement yourself around of different type of collections.
For example if you need to add new element to the end of an arraylist you have to look at the size of the collection and then add that new element at n+1 place. In linkedlist you do it directly by addLast(E e) method.
Another example. Depending on your expectations you can chose either getFirst/getLast or peekFirst/peekLast methods. Last "peek" methods are completelly new sort of methods and introduced in Java 1.6.
They slightly differs from set/get methods - they do not throw any kind of exceptions if this list is empty, return just null.
New
"poll" methods - pollFirst and pollLast combine two methods: get and
remove an element from your collection. For example pollFirst() method
gets and removes the first element from this list. This method does
throw any kind of exception like IndexOutOfBoundsException in case of
arraylist, instead it just returns null (if this list is empty).
Because the linkedlist implements queue interface you can pop and push new elements from/into your collection.
Because the linkedlist implements queue interface you can pop and push new elements from/into your collection.
If
you created capacity restricted collection you can "examine" the result
of an operation by using offerFirst/offerLast methods. They are also
new (since Java 1.6) and return boolean result on the operations instead
of throwing IllegalStateException as addFirst/addLast methods do. In
case if possible to insert an element at the beginning/end of collection
you you get "true" result.
When you write your code especially collections require some loops, complex logic or just much code. In such case to avoid code errors you can use Checkstyle Java tool.
When you write your code especially collections require some loops, complex logic or just much code. In such case to avoid code errors you can use Checkstyle Java tool.
No comments:
Post a Comment