1. Introduction to ArrayList Dynamic array in Java, part of the Java Collections Framework. Resizable, unlike standard arrays. Stores objects, not primitive types (e.g., use Integer instead of int ). Maintains insertion order. Allows duplicate elements. Can contain null values. Declaration: ArrayList<Type> list = new ArrayList<Type>(); 2. Common Constructors ArrayList() : Creates an empty list with an initial capacity of 10. ArrayList(int initialCapacity) : Creates an empty list with the specified initial capacity. ArrayList(Collection<? extends E> c) : Creates a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. 3. Basic Operations & Methods 3.1. Adding Elements add(E e) : Appends the specified element to the end of this list. ArrayList<String> names = new ArrayList<>(); names.add("Alice"); // names: ["Alice"] names.add("Bob"); // names: ["Alice", "Bob"] add(int index, E element) : Inserts the specified element at the specified position in this list. names.add(1, "Charlie"); // names: ["Alice", "Charlie", "Bob"] 3.2. Accessing Elements get(int index) : Returns the element at the specified position in this list. String first = names.get(0); // "Alice" String second = names.get(1); // "Charlie" 3.3. Modifying Elements set(int index, E element) : Replaces the element at the specified position in this list with the specified element. names.set(0, "Alicia"); // names: ["Alicia", "Charlie", "Bob"] 3.4. Removing Elements remove(int index) : Removes the element at the specified position in this list. names.remove(2); // names: ["Alicia", "Charlie"] (removes "Bob") remove(Object o) : Removes the first occurrence of the specified element from this list, if it is present. names.remove("Charlie"); // names: ["Alicia"] clear() : Removes all of the elements from this list. names.clear(); // names: [] 3.5. Size and Checks size() : Returns the number of elements in this list. int count = names.size(); // 0 if cleared isEmpty() : Returns true if this list contains no elements. boolean empty = names.isEmpty(); // true if cleared contains(Object o) : Returns true if this list contains the specified element. names.add("David"); boolean hasDavid = names.contains("David"); // true 3.6. Iterating through ArrayList For-each loop: for (String name : names) { System.out.println(name); } Standard for loop: for (int i = 0; i < names.size(); i++) { System.out.println(names.get(i)); } 4. Time Complexity (Amortized Average) Operation Time Complexity add(E e) $O(1)$ add(int index, E e) $O(N)$ get(int index) $O(1)$ set(int index, E e) $O(1)$ remove(int index) $O(N)$ remove(Object o) $O(N)$ contains(Object o) $O(N)$ size() $O(1)$ 5. Simple Program Example import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { // Create an ArrayList of Integers ArrayList<Integer> numbers = new ArrayList<>(); // Add elements numbers.add(10); numbers.add(20); numbers.add(30); System.out.println("ArrayList after additions: " + numbers); // Output: [10, 20, 30] // Add an element at a specific index numbers.add(1, 15); System.out.println("ArrayList after adding 15 at index 1: " + numbers); // Output: [10, 15, 20, 30] // Get an element int firstElement = numbers.get(0); System.out.println("First element: " + firstElement); // Output: 10 // Modify an element numbers.set(3, 35); System.out.println("ArrayList after setting index 3 to 35: " + numbers); // Output: [10, 15, 20, 35] // Remove an element by index numbers.remove(2); System.out.println("ArrayList after removing element at index 2: " + numbers); // Output: [10, 15, 35] // Check size and if empty System.out.println("Current size of ArrayList: " + numbers.size()); // Output: 3 System.out.println("Is ArrayList empty? " + numbers.isEmpty()); // Output: false // Iterate through the ArrayList System.out.println("Elements using for-each loop:"); for (int num : numbers) { System.out.println(num); } // Check if an element exists System.out.println("Does ArrayList contain 15? " + numbers.contains(15)); // Output: true System.out.println("Does ArrayList contain 100? " + numbers.contains(100)); // Output: false // Clear all elements numbers.clear(); System.out.println("ArrayList after clear: " + numbers); // Output: [] System.out.println("Is ArrayList empty after clear? " + numbers.isEmpty()); // Output: true } } 6. Important LeetCode Questions Two Sum (Easy): Find two numbers in an array/list that add up to a specific target. Often solved with a HashMap, but understanding list indexing is key. Remove Duplicates from Sorted Array (Easy): Modify an array in-place to remove duplicates. While for arrays, the concept of element shifting applies to ArrayLists too. Merge Sorted Array (Easy): Merge two sorted arrays into one sorted array. Requires careful management of indices, similar to ArrayList operations. Plus One (Easy): Given a large integer represented as an array of digits, increment it by one. Involves manipulating digits and potentially resizing (like ArrayList). Fizz Buzz (Easy): Return a list of strings for numbers 1 to $n$. Good for practicing list population. Container With Most Water (Medium): Given $n$ non-negative integers representing an elevation map, find two lines that together with the x-axis form a container, such that the container contains the most water. Uses two-pointer approach on an array/list. 3Sum (Medium): Find all unique triplets in the array which gives the sum of zero. Involves sorting and two-pointer techniques over lists.