Understanding Java ArrayList for DSA

ArrayList is a fundamental part of the Java Collections Framework, providing a dynamic array implementation of the List interface. It allows for storing and manipulating a collection of elements, with the ability to grow and shrink dynamically as needed.

Key Characteristics

  1. Dynamic Array: Unlike arrays, ArrayList can dynamically increase or decrease in size as elements are added or removed.

  2. Ordered Collection: Maintains the insertion order of elements.

  3. Null Elements: Allows the storage of null values.

  4. Duplicates: Allows duplicate elements.

  5. Not Synchronized: By default, ArrayList is not thread-safe. For concurrent access, use Collections.synchronizedList or CopyOnWriteArrayList.

  6. Random Access: Supports fast random access to elements, making it efficient for retrieval operations.

Constructors

  1. ArrayList(): Constructs an empty list with an initial capacity of ten.

     ArrayList<String> list = new ArrayList<>();
    
  2. ArrayList(Collection<? extends E> c): Constructs a list containing the elements of the specified collection in the order they are returned by the collection's iterator.

     List<String> anotherList = Arrays.asList("A", "B", "C");
     ArrayList<String> list = new ArrayList<>(anotherList);
    
  3. ArrayList(int initialCapacity): Constructs an empty list with the specified initial capacity.

     ArrayList<String> list = new ArrayList<>(20);
    

Common Methods

  1. add(E e): Appends the specified element to the end of this list.

     list.add("Apple");
    
  2. add(int index, E element): Inserts the specified element at the specified position in this list.

     list.add(1, "Banana");
    
  3. get(int index): Returns the element at the specified position in this list.

     String fruit = list.get(0);
    
  4. remove(int index): Removes the element at the specified position in this list.

     list.remove(1);
    
  5. remove(Object o): Removes the first occurrence of the specified element from this list if it is present.

     list.remove("Apple");
    
  6. contains(Object o): Returns true if this list contains the specified element.

     boolean hasApple = list.contains("Apple");
    
  7. size(): Returns the number of elements in this list.

     int size = list.size();
    
  8. isEmpty(): Returns true if this list contains no elements.

     boolean isEmpty = list.isEmpty();
    
  9. clear(): Removes all of the elements from this list.

     list.clear();
    
  10. set(int index, E element): Replaces the element with the specified element at the specified position in this list.

    list.set(0, "Cherry");
    

Iteration

ArrayList can be iterated using various methods such as for-each loop, iterators, and streams.

for (String item : list) {
    System.out.println(item);
}

Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
    System.out.println(iterator.next());
}

list.forEach(System.out::println);

Performance Considerations

  1. Random Access: O(1)

  2. Insertion/Deletion at the end: O(1) amortized

  3. Insertion/Deletion at a specific index: O(n)

  4. Search: O(n)

Thank you for reading!

You can support me by buying me a book.

0
Subscribe to my newsletter

Read articles from Vineeth Chivukula directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vineeth Chivukula
Vineeth Chivukula

There's this guy who's mad about editing and programming. It's his jam, you know?