Methods, Arrays & Strings

Introduction to Arrays

15 min Lesson 5 of 14

Introduction to Arrays

So far every variable you have declared holds a single value — one int, one String, and so on. But what if you need to store the scores of thirty students, or the names of every day in the week? Creating thirty separate variables would be tedious and unmanageable. Arrays solve this by grouping multiple values of the same type under a single name.

What Is an Array?

An array is a fixed-size, ordered collection of values that all share the same data type. Think of it as a row of numbered boxes: each box holds one value, and each box has a position number called an index.

  • Every element in an array must be the same type (e.g. all int, all String).
  • The size of an array is fixed at the moment it is created — you cannot shrink or grow it later.
  • Indexes start at 0, not 1.
Zero-based indexing is used throughout Java (and most other languages). An array with 5 elements has valid indexes 0, 1, 2, 3, and 4. Index 5 does not exist.

Declaring an Array

The declaration syntax places square brackets either after the type or after the variable name. The idiomatic Java style places them after the type:

// Preferred style int[] scores; // Also valid, but less conventional int scores[];

Declaring the variable does not yet create the array — it just reserves a name that will point to an array once one is created.

Initializing an Array

There are two common ways to give an array its values.

1. Allocate with new, then assign element by element

Use new int[5] to create an array that can hold exactly 5 integers. Java fills all slots with the default value for the type — 0 for numeric types, false for boolean, and null for objects.

int[] scores = new int[5]; // five slots, all initialised to 0 scores[0] = 95; scores[1] = 87; scores[2] = 76; scores[3] = 91; scores[4] = 68; System.out.println(scores[2]); // prints 76

2. Array initialiser (declare and fill at once)

When you already know the values, you can provide them in curly braces. Java counts the values and sets the size automatically.

int[] scores = {95, 87, 76, 91, 68}; String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
Use the initialiser syntax whenever all values are known at compile time — it is shorter and less error-prone than creating an empty array and filling it manually.

Reading the Length of an Array

Every array exposes a length field (not a method — no parentheses) that returns the number of slots it was created with:

int[] scores = {95, 87, 76, 91, 68}; System.out.println(scores.length); // prints 5

length always reflects the capacity of the array, not the number of non-zero or non-null elements. An array created as new int[10] always has length equal to 10, even if you only filled three slots.

Accessing and Modifying Elements

Read or write any element using its index in square brackets:

String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; // Read System.out.println(days[0]); // Monday System.out.println(days[4]); // Friday // Modify days[2] = "WEDNESDAY"; System.out.println(days[2]); // WEDNESDAY

ArrayIndexOutOfBoundsException

The most common array mistake is using an index that does not exist — either negative or equal to (or greater than) the array length. Java throws an ArrayIndexOutOfBoundsException at runtime when this happens, and your program stops unless you handle it.

int[] numbers = {10, 20, 30}; // valid indexes: 0, 1, 2 System.out.println(numbers[3]); // CRASH: ArrayIndexOutOfBoundsException System.out.println(numbers[-1]); // CRASH: ArrayIndexOutOfBoundsException
Off-by-one errors are the classic cause. If an array has n elements, the last valid index is n - 1. Accessing array[array.length] is always out of bounds. When in doubt, double-check your index against array.length - 1.

The safe pattern is to always use length when computing the last valid index, rather than a hard-coded number:

int[] scores = {95, 87, 76, 91, 68}; // Safe: always refers to the last element System.out.println(scores[scores.length - 1]); // prints 68

A Complete Example

The following snippet ties everything together: declare, initialise, read the length, access elements, and update a value.

public class ArrayDemo { public static void main(String[] args) { // Declare and initialise int[] temperatures = {22, 25, 19, 30, 28}; // Read length System.out.println("Days recorded: " + temperatures.length); // 5 // Access first and last element safely System.out.println("First day: " + temperatures[0]); // 22 System.out.println("Last day: " + temperatures[temperatures.length - 1]); // 28 // Modify one element temperatures[2] = 21; System.out.println("Updated third day: " + temperatures[2]); // 21 } }

Summary

Arrays let you store many values of the same type under one name. Declare them with type[], create them with new type[size] or an initialiser, and access elements with a zero-based index. Use .length to query the size, and always keep your indexes between 0 and length - 1 to avoid ArrayIndexOutOfBoundsException. The next lesson builds on this foundation by showing you how to loop over arrays and apply common manipulations.