ArrayList

implements List and RandomAccess

  • Underlying Data Structure: Resizable Array
  • Duplicates - Allowed
  • Insertion Order - Preserved
  • null insetion - Allowed
  • Best choice for: Frequent Retrieval (Random Access)
  • Worst choice for: Frequent Insertion/deletion in middle -> Several shift operations are required
  • Constructors:
    1. ArrayList l = new ArrayList();
    2. ArrayList l = new ArrayList(int initialCapacity);
    3. ArrayList l = new ArrayList(Collection C);
  • Default initial capacity: 10
  • Load factor/fill ratio:
  • New Capacity: 1.5C + 1

References are stored contiguously (not the objects).

Comparison with Arrays

data.structure.primary.array (Private)ArrayList
DefinitionAn array is a dynamically-created object. It serves as a container that holds the constant number of values of the same type. It has a contiguous memory location.The ArrayList is a class of Java Collections framework.
SizeStatic - fixed-length data structureDynamic - variable length data structure - can resize itself when needed
Size specificationMandatory to provide size while initializingSize can be left unspecified - default initial capacity will be used
PerformanceFixed size => FasterInternally backed by array. Resize operation slows down the performance
Primitive/generic typeCan store both primitives and objectsCan't store primitives - automatically converts them to objects. For example, int is converted to Integer.
Getting the lengtharr.lengtharr.size()
Adding elementsCan add elements using assignment operatoradd() function
Multiple dimensionsCan be multi-dimensionalCAN'T be multi-dimensional

Backlinks