ArrayList (raw) , ArrayList<E> (Generics)

java.util.ArrayList<E> allows for expandable arrays, and is basically the same as the older the Collections Vector class. An ArrayList has these characteristics:

  • An ArrayList automatically expands as data is added.
  • Access to any element of an ArrayList is O(1). Insertions and deletions are O(N).
  • An ArrayList has methods for inserting, deleting, and searching.
  • An ArrayList can be traversed using a foreach loop, iterators, or indexes.


Arrays or ArrayList? Programmers are frequently faced with the choice of using a simple array or an ArrayList. If the data has a known number of elements or small fixed size upper bound, or where efficiency in using primitive types is important, arrays are often the best choice. However, many data storage problems are not that simple, and ArrayList (or one of the other Collections classes) might be the right choice.
Automatic expansion. Use ArrayList when there will be a large variation in the amount of data that you would put into an array. Arrays should be used only when there is a constant amount of data. For example, storing information about the days of the week should use an array because the number of days in a week is constant. Use an array list for your email contact list because there is no upper bound on the number of contacts.

Objects only. A possible disadvantage of ArrayList is that it holds only object types and not primitive types (eg, int). To use a primitive type in an ArrayList, put it inside an object or use of the wrapper classes (eg, Integer, Double, Character, ...). The wrapper classes are immutable, so if you use, eg, Integer, you will not be able to change the integer value. In this case it may be more useful to define your own mutable class.

Implementation. ArrayLists are implemented with an underlying array, and when that array is full and an additional element is added, a new, larger, array is allocated and the elements are copied from the old to the new. Because it takes time to create a bigger array and copy the elements from the old array to the new array, it is a slightly faster to create an ArrayList with a size that it will commonly be when full. Of course, if you knew the final size, you could simply use an array. However, for non-critical sections of code programmers typically don't specify an initial size.

Comparing Vector with ArrayList


  Vector  ArrayList
  1. Yes
  2. The contents are thread safe
  3. Here Vector is slower
  1. NO
  2. The contents are not thread safe.
  3. Here ArrayList is Faster
Stretchable (capacity)
  • Both arranges the data internally and expands if run our of the room.
increases by double or two fold increases by 50 per cent
For an unknown case, Vector is better logically.  
Adding in the middle
  • Both maintain data in ordered fashion,  positional indexes are added with the objects.
  • Both will take a toll, as the removal will increase or decrease the indices from the reference point of addition or removal respectively.
  • Both may affect the speed when accessing  elements in the middle.
  • Consider LinkedList for adding and removing elements very frequently.