Skip to main content
The new Teacher Workspace is here. Your first 3 assignments are free. Try it →
A colleague sent you $30 off Fiveable. Get the annual plan for $99 →

1D array in AP Computer Science A

In AP Computer Science A, a 1D (one-dimensional) array is an object that stores a fixed number of values of the same type in a single ordered row, where each element is accessed by an index from 0 to length - 1.

Verified for the 2027 AP Computer Science A exam•Last updated October 2026

What is 1D array?

A 1D array is a single row of boxes, all holding the same type of data. That data can be primitives like int or double, or object references like String. Each box has a number called an index, starting at 0. So in an array of 5 elements, the indexes run 0 through 4.

The CED (EK 4.3.A.1 through 4.3.A.4) pins down a few rules you need cold. First, an array's length is set when you create it and can never change. You read it with the length attribute (no parentheses, so arr.length, not arr.length()). Second, if you create an array with new, like int[] scores = new int[5];, every element starts at its default value. That's 0 for int, 0.0 for double, false for boolean, and null for any reference type. Third, you can skip new and use an initializer list, like int[] nums = {4, 8, 15};, which creates the array and fills it in one step.

Why 1D array matters in AP® Computer Science A

1D arrays live in Unit 4: Data Collections, starting at Topic 4.3 (Array Creation and Access). They directly support learning objective AP Comp Sci A 4.3.A, which asks you to develop code that represents collections of related data using one-dimensional array objects.

But the 1D array doesn't stay in 4.3. It's the foundation for Topic 4.12 (Traversing 2D Arrays) too. Learning objective AP Comp Sci A 4.12.A relies on EK 4.12.A.1, which says 2D arrays are stored as arrays of arrays. That means every row of a 2D array is itself a 1D array. If you understand 1D arrays well, 2D arrays are mostly just "do that again, inside a loop." Arrays also show up all over the exam in traversal algorithms like summing, finding a max, or checking for duplicates.

How 1D array connects across the course

2D Arrays and Nested Traversal (Unit 4)

A 2D array is literally a 1D array whose elements are other 1D arrays. So grid[r] hands you an entire row as a 1D array, and grid[r][c] reaches into that row for one value. That's why EK 4.12.A.1 says traversing a 2D array works a lot like traversing a 1D one. You just nest one loop inside another.

Default Values and boolean Arrays (Unit 4)

When you write new boolean[10], you get ten false values, not ten empty slots. This matters for code that counts or tracks things. A fresh int[] of counters already starts at 0, so you can go straight to counts[i]++ without filling it first.

Enhanced For Loop (Unit 4)

The enhanced for loop (for (int x : arr)) walks through a 1D array without touching indexes. It's clean for reading values, but you can't use it to change what's stored in a primitive array or to know which index you're on. In a 2D array, the outer enhanced loop variable is a whole row, so its type is a 1D array like int[].

Index and ArrayIndexOutOfBoundsException (Unit 4)

Valid indexes run from 0 to length - 1. Ask for arr[arr.length] and Java throws an ArrayIndexOutOfBoundsException. Most off-by-one bugs on the exam come from a loop condition written with <= when it should be <.

Is 1D array on the AP® Computer Science A exam?

1D arrays show up constantly in multiple-choice questions, and often in ways you don't expect.

  • Recognizing syntax. Some questions just ask which snippet creates a one-dimensional array. You need to tell int[] a = new int[3]; and int[] a = {1, 2, 3}; apart from invalid forms or 2D declarations like int[][].
  • Arrays as rows of a 2D array. One common stem asks what type the outer loop variable must be in nested enhanced for loops over a 2D array. The answer is a 1D array type (for an int[][], it's int[]), because each row is a 1D array.
  • A 1D array as a result holder. A favorite pattern builds a 1D array sized to the number of columns, like int[] sums = new int[vals[0].length];, and then accumulates column totals inside nested loops. For example, with vals = {{1, 0, 1}, {0, 2, 0}}, adding vals[r][c] into sums[c] gives {1, 2, 1}. To get this right you need to know the array starts filled with 0s and that vals[0].length is the column count.
  • Tracing and FRQ code. No released FRQ has used the phrase "1D array" verbatim. Still, writing loops that traverse, update, or build arrays is core to the free-response section, so expect to write this code yourself.

1D array vs 2D array

A 1D array is a single row accessed with one index (arr[i]). A 2D array is an array of 1D arrays accessed with two indexes (grid[r][c]). The confusing part is that a 2D array contains 1D arrays. So grid[r] alone is a 1D array (a whole row), grid.length is the number of rows, and grid[0].length is the number of columns. If an expression has one bracket on a 2D array, you're holding a row, not a single value.

Key things to remember about 1D array

  • A 1D array stores multiple values of the same type, either primitives or object references, in a single indexed row.

  • An array's length is fixed when it's created, and you access it with the length attribute, written without parentheses.

  • Arrays created with new start filled with default values, which are 0 for int, 0.0 for double, false for boolean, and null for reference types.

  • Valid indexes run from 0 to length - 1, and using any index outside that range throws an ArrayIndexOutOfBoundsException.

  • Every row of a 2D array is a 1D array, so the outer variable in a nested enhanced for loop over an int[][] has type int[].

  • A common exam pattern uses a new 1D array to store results, such as column sums, computed while traversing a 2D array.

Frequently asked questions about 1D array

What is a 1D array in AP Computer Science A?

A 1D array is an object that holds a fixed number of values of the same type in one ordered row, with each value accessed by an index starting at 0. It's introduced in Topic 4.3 and supports learning objective AP Comp Sci A 4.3.A.

Can you change the size of an array in Java after you create it?

No. EK 4.3.A.2 says an array's length is set at creation and cannot be changed. If you need more room, you have to create a new, bigger array and copy the values over.

Is a new int array empty or filled with zeros?

It's filled with zeros. Creating new int[5] gives you {0, 0, 0, 0, 0}, because elements start at their default values (0.0 for double, false for boolean, null for objects).

How is a 1D array different from a 2D array?

A 1D array uses one index (arr[i]), while a 2D array uses two (grid[r][c]) and is stored as an array of 1D arrays. That's why grid[r] by itself returns an entire row, which is a 1D array.

Is it arr.length or arr.length() for arrays in Java?

For arrays it's arr.length, with no parentheses, because length is an attribute and not a method. Mixing this up with String's length() method is a classic trap in multiple-choice answer choices.