
Java is a programming language that has been widely used in the software industry for a long time, and it remains popular due to the security it offers for applications built with it.
So far, we have used variables to store individual values. This works well when dealing with a small amount of data. However, many real-world applications need to store a collection of related values. For example, an e-commerce application may need to store the prices of multiple products, a weather application may need to store temperatures recorded throughout the week, or an analytics system may need to store monthly sales figures.
Creating a separate variable for every value quickly becomes difficult to manage. To solve this problem, Java provides arrays.
An array allows multiple values of the same data type to be stored under a single variable name. Each value in the array can be accessed using its position, known as an index.
In this chapter, you will learn how to create arrays, access elements, modify values, iterate through arrays, and perform common array operations.
An array is a collection of multiple values stored under a single variable name.
Without an array:
double januarySales = 12000; double februarySales = 15000; double marchSales = 18000; double aprilSales = 17000;
With an array:
double[] monthlySales = {12000, 15000, 18000, 17000};
Instead of creating multiple variables, all values are stored in a single array.
Syntax
dataType[] arrayName = new dataType[size];
Example
int[] numbers = new int[5];
This creates an array that can store 5 integer values.
Values can also be provided when the array is created.
Example
double[] monthlySales = {12000, 15000, 18000, 17000};
This creates an array containing four sales values.
Each element in an array has an index.
Indexes always start from 0.
Value : 12000 15000 18000 17000 Index : 0 1 2 3
The first element is stored at index 0, the second at index 1, and so on.
Array elements are accessed using their index.
Example
double[] monthlySales = {12000, 15000, 18000, 17000};
System.out.println(monthlySales[0]);
Output
12000.0
Java retrieves the value stored at index 0.
Example
double[] monthlySales = {12000, 15000, 18000, 17000};
System.out.println(monthlySales[2]);
Output
18000.0
Since 18000 is stored at index 2, that value is displayed.
Values inside an array can be modified.
Example
double[] monthlySales = {12000, 15000, 18000, 17000};
monthlySales[1] = 20000;
System.out.println(monthlySales[1]);
Output
20000.0
The value at index 1 is replaced with 20000.
The length property returns the number of elements in an array.
Example
double[] monthlySales = {12000, 15000, 18000, 17000};
System.out.println(monthlySales.length);
Output
4
Arrays are commonly processed using loops.
Example
double[] monthlySales = {12000, 15000, 18000, 17000};
for(int i = 0; i < monthlySales.length; i++) {
System.out.println(monthlySales[i]);
}
Output
12000.0 15000.0 18000.0 17000.0
Explanation
0.Java provides a simpler way to iterate through arrays.
Syntax
for(dataType variable : arrayName) {
}
Example
double[] monthlySales = {12000, 15000, 18000, 17000};
for(double sale : monthlySales) {
System.out.println(sale);
}
Output
12000.0 15000.0 18000.0 17000.0
The enhanced for loop automatically visits each element in the array.
Example
public class Main {
public static void main(String[] args) {
double[] monthlySales = {12000, 15000, 18000, 17000};
double totalSales = 0;
for(int i = 0; i < monthlySales.length; i++) {
totalSales += monthlySales[i];
}
System.out.println("Total Sales = " + totalSales);
}
}
Output
Total Sales = 62000.0
Explanation
totalSales.Wrong
double[] sales = {12000, 15000, 18000};
System.out.println(sales[3]);
Problem
Valid indexes are:
0 1 2
Index 3 does not exist and causes an error.
Correct
System.out.println(sales[2]);
Wrong
System.out.println(sales[1]);
Expecting the first element.
Problem
Arrays start at index 0.
Correct
System.out.println(sales[0]);
0.length property returns the number of elements.