Java Arrays
A Java array stores multiple values of the same type under one variable name. Arrays are useful when you have a fixed-size list, such as scores, names, prices, or days of the week.
Each value in an array is called an element. You access elements by their index, and Java indexes start at 0.
Create An Array
To create an array, write the element type followed by square brackets, then assign values inside braces.
public class Main {
public static void main(String[] args) {
int[] scores = {90, 85, 78, 92};
System.out.println("First score: " + scores[0]);
System.out.println("Last score: " + scores[3]);
scores[2] = 80;
System.out.println("Updated third score: " + scores[2]);
}
}
Output:
First score: 90
Last score: 92
Updated third score: 80
The array has four elements, so its valid indexes are 0, 1, 2, and 3. The statement scores[2] = 80; changes the third element because index 2 means the third position.
Array Types
All elements in one Java array must have the same type. An int[] stores integers, a double[] stores decimal numbers, and a String[] stores text.
public class Main {
public static void main(String[] args) {
String[] languages = {"Java", "Python", "C++"};
System.out.println(languages[0]);
System.out.println(languages[1]);
System.out.println(languages[2]);
}
}
Output:
Java
Python
C++
Here, languages is a String[], so every element is a String. You cannot store an int in this array.
Array Length
Use arrayName.length to get the number of elements in an array. Unlike string length, array length is a field, so it does not use parentheses.
public class Main {
public static void main(String[] args) {
String[] colors = {"red", "green", "blue"};
System.out.println("Number of colors: " + colors.length);
System.out.println("Last color: " + colors[colors.length - 1]);
}
}
Output:
Number of colors: 3
Last color: blue
The last valid index is always length - 1. For an array with three elements, the indexes are 0, 1, and 2.
Loop Through An Array
Arrays are often used with loops. A regular for loop is useful when you need the index of each element.
public class Main {
public static void main(String[] args) {
int[] temperatures = {72, 75, 71, 69, 74};
for (int i = 0; i < temperatures.length; i++) {
System.out.println("Day " + (i + 1) + ": " + temperatures[i]);
}
}
}
Output:
Day 1: 72
Day 2: 75
Day 3: 71
Day 4: 69
Day 5: 74
The loop starts at 0 and continues while i < temperatures.length. This pattern stops before the index goes outside the array.
Use Arrays In Calculations
An array can hold values that you combine into a result. This example adds all values and prints the average.
public class Main {
public static void main(String[] args) {
int[] values = {3, 7, 2, 9, 4};
int total = 0;
for (int value : values) {
total += value;
}
double average = total / (double) values.length;
System.out.println("Total: " + total);
System.out.println("Average: " + average);
}
}
Output:
Total: 25
Average: 5.0
The for-each loop reads one element at a time. The cast (double) makes the division produce a decimal result instead of integer division.
Create An Empty Array
You can also create an array by giving its size. Java fills numeric arrays with 0, boolean arrays with false, and object arrays such as String[] with null.
public class Main {
public static void main(String[] args) {
int[] counts = new int[3];
counts[0] = 4;
counts[1] = 8;
counts[2] = 6;
System.out.println(counts[0]);
System.out.println(counts[1]);
System.out.println(counts[2]);
}
}
Output:
4
8
6
The size of an array is fixed after it is created. If you need a list that can grow and shrink, Java also provides collection classes such as ArrayList, which you will meet later.
Common Mistakes
- Using index
1for the first element instead of index0. - Trying to access
array[array.length], which is one past the last element. - Mixing element types in one array.
- Expecting a Java array to grow automatically after it is created.
Takeaway: a Java array stores a fixed-size sequence of same-type values, and indexes let you read, change, and loop through each element.
