Unit 6: Array

Creating, accessing, traversing, and manipulating one-dimensional arrays

Unit Resources

Select a resource below to start studying.

📚Study Guide: Array

Unit 6: Array

Arrays are the simplest data structures for storing collections of elements of the same type. In Java, arrays have a fixed length determined at creation, and each element is accessed via a zero-based index. This unit requires students to declare, initialize, and traverse arrays using both standard for loops and enhanced for loops. The AP exam extensively tests array manipulation, including finding minimum and maximum values, computing sums and averages, shifting elements, reversing arrays, and searching for specific values. Understanding that arrays are reference types--even when they hold primitives--is critical: assigning one array variable to another copies the reference, not the data, so both variables point to the same array in memory. The length field (arr.length) provides the number of elements and is commonly used as the loop bound. Students must be comfortable with common algorithms such as sequential search (O(n)) and insertion/deletion logic (shifting elements to make room or fill gaps). Array traversal patterns--forward, backward, by steps of 2--appear frequently. The AP exam often presents partially completed methods and asks students to fill in the missing code to achieve a specified result, testing both algorithmic thinking and Java syntax precision.

Key Concepts

  • Array Declaration and Creation: int[] arr = new int[10]; creates an array of 10 integers initialized to 0. int[] arr = {3, 1, 4, 1, 5}; creates and initializes in one step. Array length is fixed after creation.
  • Array Indexing: Valid indices are 0 to length-1. Accessing arr[-1] or arr[arr.length] throws ArrayIndexOutOfBoundsException.
  • Array Traversal: Use for (int i = 0; i < arr.length; i++) for index access. Use for (int val : arr) for read-only access to values. Enhanced for cannot modify elements of primitive arrays (modifies the loop variable, not the array).
  • Array as Reference Type: int[] a = {1, 2}; int[] b = a; // b points to same array as a. Changing b[0] also changes a[0]. To copy, use a loop or Arrays.copyOf().
  • Common Algorithms: Sum/average (accumulator pattern), min/max (initialize with first element), count occurrences, find index of target, reverse (swap from ends), shift elements left/right.
  • Partially Filled Arrays: An array may be larger than the number of stored elements. A companion variable (e.g., int size) tracks the logical number of elements, while arr.length tracks physical capacity.

Vocabulary

  • Element: A single value stored in an array at a specific index.
  • Index: An integer representing the position of an element in an array; Java arrays use zero-based indexing.
  • Length: The number of elements an array can hold, accessed via the length field (not a method).
  • Sequential Search: An algorithm that checks each element in order until the target is found or the end is reached; time complexity is O(n).
  • Alias: When two or more reference variables point to the same object or array in memory.
  • Accumulator: A variable used to gather or sum values during iteration, commonly used for totals and counts.

Essential Formulas / Syntax Patterns

  • int[] nums = new int[5]; // {0,0,0,0,0}
  • int[] nums = {2, 4, 6, 8};
  • for (int i = 0; i < arr.length; i++) { ... }
  • for (int i = arr.length - 1; i >= 0; i--) { ... }
  • int sum = 0; for (int x : arr) sum += x;
  • int[] copy = Arrays.copyOf(arr, arr.length);

Common Mistakes

  • ArrayIndexOutOfBoundsException: Using arr[arr.length] or a negative index. Remember valid indices are 0 to length-1.
  • Confusing Length and Size: Arrays use .length (a field, not a method). ArrayLists use .size() (a method). String uses .length() (a method).
  • Thinking Arrays Can Grow: Arrays have fixed length. To "resize," you must create a new array and copy elements.
  • Enhanced for Modification: for (int x : arr) x = 5; does NOT modify the array. It only changes the local loop variable x.

AP Exam Strategies

  • Draw Array Diagrams: Sketch the array with indices and values before and after each step of an algorithm to avoid off-by-one errors.
  • Initialize Min/Max Carefully: Set min = arr[0] (not 0) because the smallest value might be negative. Then start the loop at i = 1.
  • Use temp Variables for Swaps: int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; Never swap without a temporary variable.
  • Check Edge Cases: Empty array (length 0), single-element array, and arrays with all identical elements are common edge cases that break naive algorithms.

Real-World Applications

  • Sensor Data Logging: IoT devices store temperature or pressure readings in fixed-size arrays for averaging and threshold detection before transmission.
  • Image Buffers: Raw image data is often stored in arrays of pixel values (RGB triples) before compression or display.
  • Financial Time Series: Daily stock prices over a fixed period are stored in arrays to calculate moving averages and volatility.

Practice Quiz: Array

Answer each question one at a time. Click an option to select your answer.

Question 1 of 150
Question
Loading...
Click to flip
Answer
Loading...
Click to flip back 🔀 Shuffle
1 / 40

🎥Free Video Lessons: Array

Watch these unit review videos directly on our site.

AP Computer Science A Unit 6 Review - Arrays - For Each Loop - Traverse an Array - Algorithms - CSA by Meek Extra Help

AP CS A Unit 6 Review by Jacob MacMillan

APCS Unit 6 (Part 2): Arrays In-Depth Review and Practice Test by AP Computer Science A

📄Cheat Sheet: Array

Quick reference for Array. Print this out and review before the exam!

Unit 6: Array - Cheat Sheet

Declaration and Creation

  • int[] arr = new int[10];
  • int[] arr = {1, 2, 3};
  • Length fixed: arr.length

Traversal Patterns

  • Forward: for (int i = 0; i < arr.length; i++)
  • Reverse: for (int i = arr.length - 1; i >= 0; i--)
  • Enhanced: for (int x : arr) // read-only

Common Algorithms

  • Sum: accumulator += arr[i]
  • Max/Min: initialize with arr[0], compare
  • Search: sequential loop, break when found
  • Reverse: swap arr[i] and arr[arr.length - 1 - i]
  • Shift: move elements and fill gap

Reference Behavior

  • int[] b = a; // alias, same array
  • Copy: loop or Arrays.copyOf(arr, arr.length)

Problem-Solving Quick Reference

  • Valid indices: 0 to arr.length - 1
  • arr.length is a field, not a method
  • Enhanced for cannot modify primitive array elements
  • Swap requires a temporary variable

🔬Ultimate Review Packet Materials

Download official review materials for this unit.

No URP materials available for this unit yet.

Check back soon for study guides, practice questions, and review videos.

← Back to AP Computer Science A