Up until now, every program we have written has stored data using individual variables:
int mark1;
int mark2;
int mark3;This works for small tasks, but quickly becomes unmanageable. Imagine storing 30 student marks, or 100 temperatures, or 5000 pixel values.
A program with hundreds of separate variables is not realistic to write, fix, or maintain.
To solve this, Java gives us arrays.
An array lets us store a list of items of the same data type, all under one variable name, accessed by a number called an index.
Consider an array called grades that stores 5 independent marks:
Index: 0 1 2 3 4
grades: 75 81 67 85 91
grades[0]is 75grades[1]is 81grades[2]is 67grades[3]is 85grades[4]is 91
This indexing system is the foundation of how arrays work:
index 0 is always the first element, index 1 the second, and so on.
Arrays allow our programs to scale from tiny examples to real-world workloads.
Consider a program meant to manage marks for a class of 30 students. It must:
- store 30 marks,
- compute the average,
- print any mark upon request.
Before arrays, you would need 30 separate variables.
With arrays, you can use just one variable holding 30 elements.
This is the first example of a true data structure — a way of organizing data so programs can process it efficiently.
An array is:
- a list of values,
- all values must have the same data type,
- each value is stored in an element,
- each element is accessed using an index,
- indices start at 0.
Examples:
int[] absences— list of student absencesdouble[] temperatures— list of temperaturesString[] names— list of names
Visualization:
names
+---------+---------+---------+---------+
| "Amy" | "Chris" | "Holly" | "Ben" |
+---------+---------+---------+---------+
index 0 index 1 index 2 index 3
Accessing:
System.out.println(names[2]); // HollyRecall the practice of declaring and assigning a variable in one command:
int mark1 = 90;This line does two things:
- declares the variable (
int mark1;) - initializes it with a value (
mark1 = 90;)
Arrays follow this same two-step pattern.
Declaring an array only introduces the variable name and the type of items it will store:
int[] marks;
String[] names;
double[] menuPrices;Notes:
- The base type (
int,String, etc.) tells Java what each element will contain. - The
[]means the variable will store a list of values. - Declaring an array does not create the list yet.
At this stage, the variable exists but it points to nothing.
This is similar to:
int mark1; // declared but uninitializedIf you try to use marks[0] now, the program will crash because the array has not been created.
To actually make the list in memory, you use new:
marks = new int[30];This creates an array with 30 integer elements, all automatically set to default values.
This is similar to:
mark1 = 90; // assigns a valueThe recommended practice is to combine the declaring and creating on one line:
int[] marks = new int[30];
String[] students = new String[5];This style is clearer because it prevents errors that occur when the array is declared but never created.
The default value for each element depends on the array's type:
0for numeric arraysfalsefor boolean arraysnullfor String/other object arrays
Examples:
int[] scores = new int[4];
// [0, 0, 0, 0]
boolean[] hasPassport = new boolean[4];
// [false, false, false, false]Once an array has been declared, setting values can be done in a similar fashion as assigning variables.
Don't forget that arrays begin with a zero index for the first element.
marks[0] = 75;
marks[1] = 81;
marks[2] = 67;General pattern:
arrayName[index] = value;
Sometimes you already know exactly what values the array should contain.
In these cases, Java provides a shortcut called an array initializer.
Consider:
int[] absences = {4, 3, 6, 8, 9};This one line performs three steps at once:
-
Declares the array variable
int[] absences;
-
Creates the array with the correct size
(Java counts 5 values → size becomes 5)absences = new int[5];
-
Assigns each element its value
absences[0] = 4; absences[1] = 3; absences[2] = 6; absences[3] = 8; absences[4] = 9;
Putting all three steps together:
int[] absences = {4, 3, 6, 8, 9};Without an initializer:
int[] scores = new int[4];
scores[0] = 99;
scores[1] = 95;
scores[2] = 95;
scores[3] = 90;With an initializer (all three steps combined):
int[] scores = {99, 95, 95, 90};int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
System.out.println(daysInMonth[3]); // April = 30Use an initializer when:
- the full list is known in advance
- the list will not change during the program
Do not use an initializer when:
- values come from user input
- values are calculated later
- the size is not known ahead of time
System.out.println(marks[2]);
System.out.println(names[0]);
System.out.println(menuPrices[menuPrices.length - 1]);Index may be:
- a literal (
marks[4]) - a variable (
marks[i]) - an expression (
marks[x+1])
Remember:
nth element is at index n - 1
Every array has a built-in field called length that tells you how many elements the array contains:
System.out.println(marks.length);This is essential when writing loops, because it prevents “going past the end” of the array.
If an array has 5 elements, the valid indices are:
0 1 2 3 4
So accessing:
array[5];will cause an ArrayIndexOutOfBoundsException, one of the most common errors when working with arrays.
For example, always use length when looping:
for (int i = 0; i < marks.length; i++) {
System.out.println(marks[i]);
}This guarantees you stay within valid bounds.
Arrays use a field, so you write array.length with no parentheses.
Strings use a method, so you write string.length() with parentheses.
int[] marks = new int[5];
System.out.println(marks.length); // array → field
String name = "Chris";
System.out.println(name.length()); // String → methodEach of the following problems strengthens your understanding of array creation, indexing, assignment, and basic data manipulation.
Annotated solutions to these problems can be found here.
Create an int[] with 5 values representing cans collected by different homerooms.
Print the first and last values in the array.
Create a String[] storing your 5 favourite songs.
Print the song in the middle of the list.
Create a double[] of 6 menu prices using an array initializer.
Print the second-last price in the list.
Given the array:
int[] nums = {4, 8, 15, 16, 23, 42};Replace the value at index 2 with a new number.
Print all values using a loop.
Create an integer array of 5 elements.
Swap the first element with the last.
Print the array before and after the swap.
Create a String[] with 4 names.
Create a second empty String[] of the same size.
Copy each element from the first array into the second one manually, using direct index access.
Given:
double[] data = {1.5, 3.2, 4.8, 7.6, 9.1};Print the first element, the last element, the middle element, and one additional element chosen using an expression involving data.length.
Ask the user to enter five names.
Store them inside a String[] of size 5.
Then print the names in the following order:
- the third name entered
- the first name entered
- the last name entered
- the second name entered
- the middle name in the list
Think carefully about the array positions. Try to write your solution so that it is valid for an array of any size, not just hardcoded as 5.
Given the array:
int[] values = {12, 5, 9, 20, 7};Create a second integer array that stores the difference between neighbouring elements in values.
For example, the first element of your new array should represent the difference between values[0] and values[1], the second element should represent the difference between values[1] and values[2], and so on.
Requirements:
- the new array should have a size based on
values.length - each position in the new array should store one neighbour difference
- print out both arrays with clear labelling so it is obvious which differences belong to which pair of values
Sample structure (your exact wording may differ):
Original: 12 5 9 20 7
Diffs: 7 4 11 13
Think carefully about how the indices of the new array relate to the indices of the original array.
Given two related arrays:
String[] players = {"Ana", "Ben", "Ming", "Hermione"};
int[] scores = {14, 22, 18, 31};Print a properly aligned table where each player's name appears next to their score.
Requirements:
- ensure the names are padded so that the scores line up neatly
- do not assume all names are the same length
- use the relationship between the arrays (same index → same player)
Your output should match the structure of the sample below:
Ana : 14
Ben : 22
Ming : 18
Hermione : 31
Spacing must adjust automatically based on string lengths.