Summary: in this tutorial, you will learn how to use Java array to store multiple values of the same data type under a single variable name.
Introduction to Java array
So far, you have learned how to declare variables that hold a single value e.g., an integer, a double, and a string.
In Java, an array is a data structure that allows you to store multiple values of the same data type under a single variable. Arrays allow you to manage collections of data efficiently.
Declaring an array
To declare an array, you specify the data type of the elements it will hold, followed by square brackets []
and the array name:
dataType [] arrayName;
Code language: Java (java)
For example, the following declares an array that stores integers:
int [] numbers;
Code language: Java (java)
Initializing an array
If you know the elements that an array will hold, you can provide the values of the elements at the time of declaration. For example, the following declares an array and initializes its elements that include 1, 2, and 3:
int[] numbers = { 1, 2, 3 };
Code language: Java (java)
Getting the array’s length
To determine the number of elements of an array, you use the length
property. For example:
public class App {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
System.out.println(numbers.length); //3
}
}
Code language: Java (java)
Output:
3
Code language: Java (java)
In this example, we use the length
property to get the number of elements of the numbers
array.
Accessing array elements
To access an array element, you can use its index. The first element of the array starts at 0. For example, the following shows how to access the first and second elements of the numbers
array:
int[] numbers = {1, 2, 3};
int first = numbers[0];
int second = numbers[1];
Code language: Java (java)
Modifying array elements
To modify an array element, you assign a value to it using the assignment operator =
. For example, the following changes the third element of the numbers array to 30:
public class App {
public static void main(String[] args) {
int[] numbers = { 1, 2, 3 };
numbers[2] = 30;
System.out.println(numbers[2]); // 30
}
}
Code language: Java (java)
Note that the third element has an index 2.
Dynamic initialization
If you don’t know the elements of an array upfront, you can specify the size of the array:
int[] numbers = new int[3];
Code language: Java (java)
Later, you can assign values to individual elements:
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
Code language: Java (java)
Iterating through arrays
To iterate through an array, you can use a for loop and index-based access. For example, the following uses the for
loop to output the elements of the numbers
array to the screen:
public class App {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Code language: Java (java)
You can also use enhanced for loop (also known as for-each loop) to simplify array iteration:
public class App {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
for (int number : numbers) {
System.out.println(number);
}
}
}
Code language: Java (java)
Summary
- Use an array to store multiple values of the same data type under a single variable.
- Use the length property to get the number of elements in an array.
- Arrays are zero-based indexing, meaning the first element always starts at zero.
- Use a for loop or for-each loop to iterate over elements of an array.