Units 6-8 cover data structures — arrays (fixed size), ArrayLists (dynamic), and 2D arrays. These are central to the AP exam, especially traversal, search, and sort algorithms.
Declaration: int[] arr = new int[10]; or int[] arr = {1, 2, 3}; Access: arr[index] (0-based). Length: arr.length (no parentheses). ArrayIndexOutOfBoundsException if index ≥ length or < 0. Traversal: for (int i = 0; i < arr.length; i++) or enhanced for (int val : arr). Arrays are fixed size; cannot add/remove elements.
ArrayList<Type>: dynamic size, stores objects only (no primitives — use Integer, Double). Methods: add(item), add(index, item), get(index), set(index, item), remove(index), size(). Remove during traversal: iterate backwards (for i = size()-1; i >= 0; i--) to avoid skipping. Enhanced for loop cannot modify list structure. Autoboxing/unboxing: automatic conversion between primitives and wrapper classes.
2D array: int[][] grid = new int[rows][cols]. Access: grid[row][col]. Row traversal: for(int r=0; r<grid.length; r++) for(int c=0; c<grid[0].length; c++). Column-major: swap loop order. Linear search: O(n). Binary search: O(log n), requires sorted array. Selection sort: find min, swap to front, repeat — O(n²). Insertion sort: insert each element into sorted portion — O(n²). Merge sort: divide & conquer — O(n log n).
When you remove an element from an ArrayList, all elements after it shift left by one index. If you iterate forward (i=0 to size-1) and remove element at index i, element i+1 becomes the new element at index i, but i increments to i+1, skipping the shifted element. Iterating backwards (i=size()-1 to 0) avoids this because: removed elements are behind the current index, and shifting only affects elements at higher indices (already processed). Alternative: use a while loop and only increment i when you don\'t remove. The enhanced for loop throws ConcurrentModificationException if you modify the list.
Book a Trial + Diagnostic session. Get a personalized Learning Path with clear milestones, tutor match, and a plan recommendation — all within 24 hours.
Book Trial + Diagnostic →