4.3 - Array Creation and Access

mg8mer

Introduction

Welcome to the FiveHive website article for topic 4.3!

Today, our learning targets, according to AP® Computer Science A Course and Exam Description 2025, are as follows:

4.3.A Develop code used to represent collections of related data using one-dimensional (1D) array objects.

After two intro lessons, finally, we’re going to cover the meat of this unit: Arrays! In this lesson, we’re going to be discussing the basics of creating Arrays and accessing values within them.

Let’s not waste any further time and jump straight in! Get excited!

What is an Array?

An Array is more or less a list, one that can store multiple values that are OF THE SAME TYPE. These values can be primitives (like , , ), or even object references (like , , or any class)!

Think of an Array as a box of objects, each numbered sequentially starting from . Again, starting from ! This is extremely important to remember, and it is a very common misconception that Arrays start with index ; they always start with ! Yes, it’s weird, but this is Java, not real life.

You may initially think: mg8mer, why do we need to use arrays?

Well, without arrays, storing multiple related values requires individual variables:

Yeah… this approach very quickly becomes impractical with many values. Imagine you needed to store 100 values, that’s 100 variables! Fortunately, Arrays are here to save the day:

int[] scores = {85, 92, 78, 88, 95};

Look at that! See how compact? How elegant? How simple? It’s so easy to access every value now!

Creating Arrays

There are two primary ways to create arrays in Java: using the new keyword or using initializer lists. Let’s begin with the former:

The keyword creates an array of a specified size:

int[] numbers = new int[5];

Wow that’s a lot of new syntax! Don’t freakout, let’s break it down

  • → declares the type as "array of int"
  • →  variable name
  • → creates an array with 5 elements.

General syntax:

dataType[] arrayName = new dataType[size];

You now may be wondering: ok we’ve created the array, but what is in it?!?!

Well when an array is created using the keyword new, all of its elements are initialized to the default values for the element data type:

  • arrays: default value is
  • arrays: default value is
  • arrays: default value is
  • Reference type arrays (objects): default value is

Hey don’t worry, though! You can still create an array with predefined values, just like this:

int[] scores = (85, 92, 78, 88, 95};

Instead of using “new”, we just used our bracket friends to house and separate our numbers using commas!

Array Length

REMEMBER!!! Once you create an array, BANG: its length is now FIXED and CANNOT be changed.

Sorry if I scared you, but if you want to access the length of the array, you can do so through the length attribute:

int[] numbers = {10, 20, 30, 40, 50};

  → The value of “size” will be five.

NOTE: is an attribute, not a method, so there are no parentheses!

nunbers.length  // Correct!
numbers.length()  // WRONG WRONG WRONG SOUND THE ALARMS

Accessing Array Elements

Square brackets are used to access and modify an element in a 1D array using an index.

REMEMBER, for an array of size n:

  • First element: index
  • Second element: index
  • Last element: index

For example:

String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri"};
ValueMonTueWedThuFri
Index01234

Use the index in square brackets to access an element:

String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri"};
System.out.println(days[0]);  // Prints Mon
System.out.println(days[2]);  // Prints Wed
System.out.println(days[4]);  // Prints Fri

Modifying Elements

Use the same syntax to modify elements:

int[] scores = {85, 92, 78};
scores[1] = 95;  // Change second element from 92 to 95
// Array is now: {85, 95, 78}

Complete example:

int[] numbers = new int[3];  // Creates {0, 0, 0}
numbers[0] = 10;  // {10, 0, 0}
numbers[1] = 20;  // {10, 20, 0}
numbers[2] = 30;  // {10, 20, 30}

Using Variables as Indices

Indices can be variables or expressions:

int[] values = (5, 10, 15, 20, 25};
int index = 2;
System.out.println(values[index[);  // Prints 15
System.out.println(values[index + 1]);  // Prints 20
System.out.println(values[index * 2]);  // Prints 25

Valid Index Range

The valid index values for an array are 0 through one less than the length of the array, inclusive.

For an array with length n:

  • Valid indices: 0, 1, 2 … n - 1 (because the first index is 0!)
  • Invalid indices: Any negative number, n, or any value greater than n

Using an index value outside of the valid range will result in an .

For example:

int[] numbers = {10, 20, 30};  // Length is 3, valid indices: 0, 1, 2
System.out.println(numbers[0]);  // OK: 10
System.out.println(numbers[2]);  // OK: 30
System.out.println(numbers[3]);  // ERROR: ArrayIndexOutOfBoundsException
System.out.println(numbers[-1]);  // ERROR: ArrayIndexOutOfBoundsException

Array Declaration Variations

Java allows different syntax styles for array declarations:

int[] numbers;  // Preferred style
int numbers[];  // Also valid, but less common

Both create an array variable, but the first style () is preferred because it clearly shows that int[] is the type.

Alright, enough yappage. Let’s see if you can apply what your nogging has been absorbing!

Practice