How do you create an array of a fixed size, and how do you read or change the element at a given index?
Topic 6.1 Array Creation and Access: declare and initialise a one-dimensional array, access elements by index from 0 to length minus 1, and use the length attribute.
A focused answer to AP CSA Topic 6.1, covering how to declare and create a one-dimensional array, default element values, the length attribute, accessing and modifying elements by index, the valid index range, and ArrayIndexOutOfBoundsException, with a fully worked example.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 6.1) wants you to create a one-dimensional array and access its elements. An array is a fixed-size, ordered collection of values that all share one type. You store the array in a single variable and reach each value by its index, an integer position that starts at 0. You must know how to declare an array, how it is sized, what its elements start as, and how to read and write an element safely within the valid index range.
Declaring and creating an array
int[] scores = new int[4]; // 4 int slots, all 0
double[] prices = new double[3];
String[] names = new String[2]; // both null
The type int[] means "array of int". The new int[4] part allocates the storage and fixes the length at 4. You can also create an array from an initialiser list, which sets the values and the length at once:
int[] data = {5, 10, 15, 20}; // length 4, no new needed
Default values
When you create an array with new, every element starts at the default value for its type: 0 for int, 0.0 for double, false for boolean, and null for any object type such as String. An initialiser list instead fills the elements with the values you write.
The length attribute
int[] a = {3, 6, 9};
System.out.println(a.length); // 3
Accessing and modifying elements
Use square brackets with an index to read or write a single element:
int[] a = {3, 6, 9};
int first = a[0]; // read: first is 3
a[2] = 99; // write: a is now {3, 6, 99}
Valid indices run from 0 (the first element) to a.length - 1 (the last). Index a.length is one past the end and is invalid.
Try this
Q1. What is the last valid index of an array created with new int[10]? [1 point]
- Cue. Index 9, because indices run from 0 to
length - 1, andlengthis 10.
Q2. Write one statement that declares and creates a double array named temps holding exactly 7 elements, all starting at 0.0. [2 points]
- Cue.
double[] temps = new double[7];- thenew double[7]fixes the length at 7 and defaults each element to 0.0.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2022 (style)1 marksMultiple choice. Consider the following code segment.
```java
int[] arr = new int[5];
arr[0] = 10;
arr[2] = 20;
System.out.println(arr[1] + arr[2]);
```
What is printed?
(A) `20`
(B) `30`
(C) `10`
(D) `0`
(E) An `ArrayIndexOutOfBoundsException` is thrown.
Show worked answer →
The answer is (A).
new int[5] creates an array of 5 int elements, each set to the default value 0. The code assigns arr[0] = 10 and arr[2] = 20, but never assigns arr[1], so arr[1] is still 0. Therefore arr[1] + arr[2] is 0 + 20 = 20. (B) would add arr[0]; (C) ignores arr[2]; (D) assumes both are still default; (E) is wrong because index 2 is valid for a length-5 array.
Markers reward knowing that numeric array elements default to 0 and that an unassigned element keeps that default.
AP 2021 (style)4 marksFree response (code writing). An `int` array `data` has been declared and initialised with values. Write a code segment that creates a new `int` array called `copy` of the same length as `data`, then copies every element of `data` into `copy` so that `copy[i]` equals `data[i]` for each valid index.
Show worked answer →
A 4-point question testing array creation sized from length and an index-based copy loop.
int[] copy = new int[data.length];
for (int i = 0; i < data.length; i++) {
copy[i] = data[i];
}
Point 1: create copy with new int[data.length] so it matches the size of data. Point 2: loop the index i from 0 to data.length - 1 with i < data.length. Point 3: assign copy[i] = data[i] inside the loop. Point 4: use valid indices only (no off-by-one). Using i <= data.length would access an invalid index and throw an ArrayIndexOutOfBoundsException.
Related dot points
- Topic 6.2 Traversing Arrays: use a standard for loop with an index variable to traverse an array, reading or modifying each element, while staying inside the valid index bounds.
A focused answer to AP CSA Topic 6.2, covering how to traverse a one-dimensional array with a standard indexed for loop, the correct loop bounds, reading versus modifying elements, partial traversals, and avoiding off-by-one errors and out-of-bounds exceptions, with a fully worked example.
- Topic 6.3 Enhanced for Loop for Arrays: use an enhanced for-each loop to traverse the elements of an array, and explain why it cannot modify the array or access indices.
A focused answer to AP CSA Topic 6.3, covering the enhanced for-each loop syntax for arrays, how the loop variable holds a copy of each element, why it cannot change array elements or give the index, and when to use it versus a standard indexed loop, with a fully worked example.
- Topic 6.4 Developing Algorithms Using Arrays: write and apply standard algorithms over a one-dimensional array, including finding minimum, maximum, sum, average, count and shifting or rearranging elements.
A focused answer to AP CSA Topic 6.4, covering the standard array algorithms - sum, average, minimum, maximum, count, search, and rearranging elements - the accumulator and running-best patterns, and how to combine them, with a fully worked example.
- Topic 4.2 for Loops: use a for loop, whose header combines initialisation, a boolean condition and an update, to repeat a block a controlled number of times, and convert between for and while loops.
A focused answer to AP CSA Topic 4.2, covering for-loop header syntax (init; condition; update), the order in which the three parts run, counting iterations, equivalence with while loops, common patterns, and tracing, with a fully worked example.
- Topic 1.2 Variables and Data Types: identify the primitive types int, double and boolean, and declare, initialise and use variables of those types.
A focused answer to AP CSA Topic 1.2, covering the difference between primitive and reference types, the three exam primitives int, double and boolean, declaring and initialising variables, valid identifiers, and constants, with a traced worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)