Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

JavaFAQ Home » General Java Go to all tips in General Java

Bookmark and Share

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.

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.

The eclipse-cs Checkstyle plug-in integrates the well-known source code analyzer Checkstyle into today's leading IDE - Eclipse. Checkstyle is a development tool written by Oliver Burn to help you ensure that your Java code adheres to a set of coding standards. Checkstyle does this by inspecting your Java source code and pointing out items that deviate from a defined set of coding rules. The full Checkstyle distribution is included with the Checkstyle plug-in. In addition to using the Eclipse Checkstyle plug-in you can also use Checkstyle from the command line or as part of an Ant build. For more information about using Checkstyle outside of Eclipse see the Checkstyle do(edited)entation. With the Checkstyle Eclipse plug-in your code is constantly inspected for problems. Within the Eclipse workbench you are notified of problems via the Eclipse Problems View and source code annotations just as you would see with compiler errors or warnings.

Look at the code snippet - Usage of LinkedList:


List listA = new LinkedList();
        List listB = new LinkedList();

        for (int i = 0; i < MAX; i++) {
            System.out.println("  - Storing Integer(" + i + ")");
            listA.add(new Integer(i));

        System.out.println("  - Storing String(Alex)");

        System.out.println("  - Storing String(Melody)");

        System.out.println("  - Storing String(Jeff)");

        System.out.println("  - Storing String(Alex)");

Full code example you can find, copy/paste or download here:  Java Code Example: LinkedList Example

Here is even more examples on the LinkedList Class usage in different code examples:

Java Code Examples for the Class: LinkedList

Some more info on the LinkedList (with real working code examples as well) on our site:

Collections of Objects
List functionality
Making a stack from a LinkedList, Making a queue from a LinkedList
This example implements a Map using a pair of ArrayLists

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy