Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.811 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Declaring and creating an array
  3. Default values
  4. The length attribute
  5. Accessing and modifying elements
  6. Try this

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, and length is 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]; - the new 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

Sources & how we know this