A 1D array in Java holds multiple values of the same type under one variable name, and its length is fixed when you create it. You access and change elements with square brackets and an index, where valid indices run from 0 to length - 1. For AP Computer Science A, watch array bounds closely because one off-by-one error can throw an ArrayIndexOutOfBoundsException.
Why This Matters for the AP Computer Science A Exam
Arrays are the foundation for everything else in Unit 4, which carries the largest weight on the AP Computer Science A exam. Getting array creation and indexing right sets you up for traversals, array algorithms, 2D arrays, searching, and sorting.
On the multiple-choice section, you will trace array code to predict output or spot which line throws an exception. On free-response code writing, you will create arrays, fill them, and access elements correctly, so off-by-one mistakes can cost easy points. The two skills emphasized here are writing code that uses arrays and determining the result of code that uses arrays.

Key Takeaways
- An array stores multiple values of the same type, either primitive values or object references.
- The length is set when the array is created and cannot change; read it with the
lengthattribute (no parentheses). - Using
newinitializes every element to a default:0forint,0.0fordouble,falseforboolean, andnullfor reference types. - Initializer lists like
{1, 2, 3}create and fill an array in one step. - Valid indices are
0throughlength - 1; anything outside that range throws anArrayIndexOutOfBoundsException. - Use square brackets to both read (
arr[i]) and modify (arr[i] = value) an element.
Core Concepts
Declaring and Creating Arrays
Declaring an array tells Java the element type and that the variable is an array, marked by square brackets. Creating the array with new reserves space for a fixed number of elements.
</>Java// Declaration: the brackets can follow the type or the name int[] scores; // preferred style int scores[]; // also valid // Creation: new reserves space for 7 ints, all set to 0 scores = new int[7];
The length you pass to new is locked in. You cannot grow or shrink an array later; you would have to create a new one.
Default Values
When you create an array with new but do not assign values, every element starts at the default for its type.
| Element type | Default value |
|---|---|
int | 0 |
double | 0.0 |
boolean | false |
reference type (like String) | null |
</>Javaint[] counts = new int[3]; // {0, 0, 0} double[] totals = new double[2]; // {0.0, 0.0} boolean[] flags = new boolean[2];// {false, false} String[] names = new String[2]; // {null, null}
Initializer Lists
When you already know the values, an initializer list creates and fills the array at the same time. The length is set by how many values you list.
</>Javaint[] primes = {2, 3, 5, 7, 11}; // length 5 String[] days = {"Mon", "Tue", "Wed"}; // length 3
Indexing and Access
Indexing starts at 0, so the first element is at index 0 and the last is at index length - 1. The same square bracket notation reads a value and writes a value.
</>Javaint[] nums = {10, 20, 30, 40}; int first = nums[0]; // reads 10 nums[2] = 99; // writes 99, array is now {10, 20, 99, 40} int last = nums[nums.length - 1]; // reads 40
The length Attribute
Every array has a length attribute that gives the number of elements. It is a field, not a method, so there are no parentheses.
</>Javaint[] data = new int[5]; System.out.println(data.length); // 5 System.out.println(data.length - 1); // 4, the highest valid index
length always equals the number of elements the array was created with, even if some elements still hold their default values.
Worked Examples
Example: Create, fill, and read
</>Javaint[] temps = new int[7]; // 7 zeros temps[0] = 72; temps[1] = 75; // indices 2 through 6 are still 0 System.out.println(temps[0]); // 72 System.out.println(temps[temps.length - 1]); // 0 (last element default)
Example: Initializer list with object references
</>JavaString[] subjects = {"Math", "Science", "English"}; System.out.println(subjects[1]); // Science subjects[1] = "Biology"; // modifies index 1 System.out.println(subjects[1]); // Biology System.out.println(subjects.length); // 3
Example: Spotting a bounds error
</>Javaint[] arr = {4, 8, 15, 16}; // valid indices 0..3 System.out.println(arr[4]); // ArrayIndexOutOfBoundsException
Index 4 does not exist because the highest valid index is length - 1, which is 3.
How to Use This on the AP Computer Science A Exam
Code Tracing
When you see array code, write the array out with its indices labeled 0, 1, 2, and so on. Track each assignment as it happens. If a line uses an index, check whether it falls in the range 0 to length - 1 before assuming the code runs.
Code Writing
When you create an array with new, remember the elements start at default values, so you may need a loop to fill them. When you need the last element, use arr[arr.length - 1], not arr[arr.length].
Common Trap
The most frequent slip is a loop condition that uses <= with length, which tries to read arr[arr.length] and throws an exception. Use i < arr.length to stop exactly at the last valid index.
</>Java// Correct loop bound for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
Common Misconceptions
lengthis not a method. Writearr.length, notarr.length(). It also is not the last index; the last index islength - 1.- An array's size is fixed at creation. You cannot add or remove slots later; you would create a new array.
- A declared array is not the same as a created one.
int[] nums;only declares the variable. Until you assign it (for example withnew), trying to use it as an array leads to anullreference. - An array made with
newis not empty in the sense of holding nothing. Each slot holds a default value like0ornull, which is different from an unassigned array variable. - Indices start at 0, not 1. The first element is
arr[0], and counting from 1 is a common cause of off-by-one errors. - An
ArrayIndexOutOfBoundsExceptionhappens at run time, not when the code compiles. The compiler will not catch an out-of-range index for you.
Related AP Computer Science A Guides
Vocabulary
The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.Term | Definition |
|---|---|
1D array | An array with a single row of elements, organized in a linear sequence and accessed using a single index. |
array | A data structure that stores a fixed-size collection of elements of the same type in contiguous memory locations, accessed by index. |
ArrayIndexOutOfBoundsException | An error that occurs when attempting to access an array element using an index value outside the valid range of 0 through length minus one. |
default values | The initial values automatically assigned to instance variables by the default constructor based on their data type (0 for int, 0.0 for double, false for boolean, null for reference types). |
index | A numeric position in a string, starting from 0 for the first character and going up to one less than the length of the string. |
initializer list | A syntax used to create and initialize an array with specific values at the time of creation. |
length attribute | A property of an array that indicates the number of elements it contains and cannot be changed after creation. |
object reference | A value that points to the memory location where an object is stored, allowing access to that object. |
primitive values | Basic data types in Java such as int, double, and boolean that store actual values directly. |
Frequently Asked Questions
What is AP CSA 4.3 about?
AP Computer Science A 4.3 is about creating and accessing one-dimensional arrays in Java. You learn how arrays store same-type values, how length is set at creation, how default values work, and how indices access elements.
How do you create an array in Java?
You create an array by declaring the element type and using `new`, such as `int[] scores = new int[7];`. You can also use an initializer list like `int[] nums = {1, 2, 3};` when you already know the values.
What are valid array indices in Java?
Valid array indices run from `0` through `array.length - 1`. The first element is at index `0`, and the last element is at one less than the array length.
What causes ArrayIndexOutOfBoundsException?
An `ArrayIndexOutOfBoundsException` happens when code tries to access an index outside the valid range of `0` through `array.length - 1`. Common causes are off-by-one loop conditions or using `array.length` as an index.
What are default values in a Java array?
When an array is created with `new`, its elements start at default values: `0` for `int`, `0.0` for `double`, `false` for `boolean`, and `null` for reference types such as `String`.
How do arrays show up on the AP CSA exam?
Arrays appear in multiple-choice tracing and free-response coding. Be ready to create arrays, use `length`, access and modify elements with square brackets, and avoid off-by-one index errors.