---
title: "AP CSA 4.3: Array Creation and Access"
description: "Review AP Computer Science A 4.3, including 1D array creation, Java array indexing, default values, initializer lists, length, square brackets, and ArrayIndexOutOfBoundsException."
canonical: "https://fiveable.me/ap-comp-sci-a/unit-4/array-creation-and-access/study-guide/umTe6NA38OqZOhMZjFWi"
type: "study-guide"
subject: "AP Computer Science A"
unit: "Unit 4 – Data Collections"
lastUpdated: "2026-06-09"
---

# AP CSA 4.3: Array Creation and Access

## Summary

Review AP Computer Science A 4.3, including 1D array creation, Java array indexing, default values, initializer lists, length, square brackets, and ArrayIndexOutOfBoundsException.

## Guide

A [1D array](/ap-comp-sci-a/key-terms/1d-array "fv-autolink") 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](/ap-comp-sci-a "fv-autolink"), 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](/ap-comp-sci-a/unit-4 "fv-autolink"), 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](/ap-comp-sci-a/key-terms/algorithm "fv-autolink"), 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](/ap-comp-sci-a/key-terms/exception "fv-autolink"). 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](/ap-comp-sci-a/key-terms/object-reference "fv-autolink").
- The length is set when the array is created and cannot change; read it with the `length` [attribute](/ap-comp-sci-a/key-terms/attribute "fv-autolink") (no parentheses).
- Using `new` initializes every element to a default: `0` for `int`, `0.0` for `double`, `false` for `boolean`, and `null` for [reference types](/ap-comp-sci-a/key-terms/reference-type "fv-autolink").
- Initializer lists like `{1, 2, 3}` create and fill an array in one step.
- Valid indices are `0` through `length - 1`; anything outside that range throws an `ArrayIndexOutOfBoundsException`.
- 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` |

```java
int[] 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](/ap-comp-sci-a/key-terms/list "fv-autolink").

```java
int[] 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.

```java
int[] 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](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz "fv-autolink"), so there are no parentheses.

```java
int[] 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](/ap-comp-sci-a/key-terms/default-values "fv-autolink").

## Worked Examples

### Example: Create, fill, and read

```java
int[] 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

```java
String[] 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

```java
int[] 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](/ap-comp-sci-a/key-terms/loop "fv-autolink") 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](/ap-comp-sci-a/key-terms/loop-condition "fv-autolink") 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

- `length` is not a method. Write `arr.length`, not `arr.length()`. It also is not the last index; the last index is `length - 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 with `new`), trying to use it as an array leads to a `null` [reference](/ap-comp-sci-a/key-terms/reference "fv-autolink").
- An array made with `new` is not empty in the sense of holding nothing. Each slot holds a default value like `0` or `null`, 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 `ArrayIndexOutOfBoundsException` happens at run time, not when the code compiles. The [compiler](/ap-comp-sci-a/key-terms/compiler "fv-autolink") will not catch an out-of-range index for you.

## Related AP Computer Science A Guides

- [4.4 Traversing Arrays](/ap-comp-sci-a/unit-4/traversing-arrays/study-guide/kRcOqfawCcBz6gcT646t)
- [4.5 Developing Algorithms Using Arrays](/ap-comp-sci-a/unit-4/developing-algorithms-using-arrays/study-guide/c6dpJfmjG7oVFDqnXFAk)
- [4.14 Searching](/ap-comp-sci-a/unit-4/searching/study-guide/8PTb7wMUSzHeI7aTUFNy)
- [4.11 2D Arrays](/ap-comp-sci-a/unit-4/2d-arrays/study-guide/5WDx6ZFeWhx2aVuiZI6R)
- [4.13 Implementing 2D Array Algorithms](/ap-comp-sci-a/unit-4/implementing-2d-array-algorithms/study-guide/9ucC5cB6ffrLnA4b3FU3)
- [4.1 Ethical and Social Implications](/ap-comp-sci-a/unit-4/ethical-and-social-implications/study-guide/iec7yzDQ2qENx5UAdiPJ)

## Vocabulary

- **1D array**: An array with a single row of elements, organized in a linear sequence and accessed using a single 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.
- **array**: A data structure that stores a fixed-size collection of elements of the same type in contiguous memory locations, accessed by index.
- **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.

## FAQs

### 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.

## Structured Data

```json
{"@context":"https://schema.org","@type":"FAQPage","inLanguage":"en","mainEntity":[{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4/array-creation-and-access/study-guide/umTe6NA38OqZOhMZjFWi#what-is-ap-csa-43-about","name":"What is AP CSA 4.3 about?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4/array-creation-and-access/study-guide/umTe6NA38OqZOhMZjFWi#how-do-you-create-an-array-in-java","name":"How do you create an array in Java?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4/array-creation-and-access/study-guide/umTe6NA38OqZOhMZjFWi#what-are-valid-array-indices-in-java","name":"What are valid array indices in Java?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4/array-creation-and-access/study-guide/umTe6NA38OqZOhMZjFWi#what-causes-arrayindexoutofboundsexception","name":"What causes ArrayIndexOutOfBoundsException?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4/array-creation-and-access/study-guide/umTe6NA38OqZOhMZjFWi#what-are-default-values-in-a-java-array","name":"What are default values in a Java array?","acceptedAnswer":{"@type":"Answer","text":"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`."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-4/array-creation-and-access/study-guide/umTe6NA38OqZOhMZjFWi#how-do-arrays-show-up-on-the-ap-csa-exam","name":"How do arrays show up on the AP CSA exam?","acceptedAnswer":{"@type":"Answer","text":"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."}}]}
```
