English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn how to use arrays in Java. We will learn about declaring, initializing, and accessing array elements through examples.
An array is a collection of similar type data. It is a container used to store data of a single type (values). For example, you can create an array that can save100 values of int type.
In Java, an array is a fundamental structure that allows you to store and access a large number of regular values.
In Java, this is how we declare an array.
dataType[] arrayName;
dataType - It can bePrimitive data typessuch as int, char, double, byte, etc., can also beJava object
arrayName - It is aIdentifier
Let's take an example
double[] data;
Here, data is an array that can save double type values.
But how many elements can it hold?
A good question! We must allocate memory for the array. Memory will define the number of elements the array can hold.
data = new Double[10];
Here, the size of the array is10. This means it can hold10elements (10double values). The size of the array is also called the length of the array.
Note: Once the length of the array is defined, it cannot be changed in the program.
Let's take another example:
int[] age; age = new int[5];
This age is an array. It can hold5int values. This age is an array. It can hold
In Java, we can declare and allocate memory for an array in a single statement. For example,
int[] age = new int[5];
In Java, each element in an array is associated with a number. This number is called an array index. We can use these indices to access array elements. For example,
int[] age = new int[5];
Here, we have an array of length5array. In the diagram, we can see that each element is represented by a number (array index). Array indices always start from 0.
Now, we can use index numbers to access array elements. For example, to access the first element of the array, we can use age[0], the second element using age[1]] access, and so on.
Note: If the length of the array is n, the first element of the array will be arrayName[0], and the last element will be arrayName[n-1]);
If we do not store any values in the array, the array will store some default values separately (0 for int type, false for boolean type). For example,
class ArrayExample { public static void main(String[] args) { //Create an array of length5array int[] age = new int[5]; //Access each element of the array using an index number System.out.println(age[0]); System.out.println(age[1}); System.out.println(age[2}); System.out.println(age[3}); System.out.println(age[4}); }; };
Output:
0 0 0 0 0
In the above example, we created an array named age. However, we did not assign any values to the array. Therefore, when we access each element of the array, the default value is printed to the screen.
Here, we access the elements of the array separately. There is a better way to use a loop (usuallyfor loop)to access array elements. For example,
class ArrayExample { public static void main(String[] args) { //Create an array of length5array int[] age = new int[5]; //Access elements using a for loop for (int i = 0; i < 5; ++i) { System.out.println(age[i]); }; }; };
Output:
0 0 0 0 0
In Java, we can initialize an array at the time of declaration, or initialize it later in the program as needed.
Below is how to initialize an array during declaration.
int[] age = {12, 4, 5, 2, 5};
This statement creates an array named age and initializes it with the values provided within the curly braces.
The length of the array is determined by the number of values separated by commas within the curly braces. In our example, the length of age is5.
Let's write a simple program to print the elements of this array.
class ArrayExample { public static void main(String[] args) { //Create an array int[] age = {12, 4, 5, 2, 5}; // Accessing array elements through array index values for (int i = 0; i < 5; ++i) { System.out.println("Element index " + i +: " + age[i]); }; }; };
Output:
Element index 0: 12 Element index 1: 4 Element index 2: 5 Element index 3: 2 Element index 4: 5
As mentioned before, we can easily access and change array elements using numeric indices. For example,
class ArrayExample { public static void main(String[] args) { int[] age = new int[5]; //Insert into the third element14 age[2] = 14; //To34Insert the first element age[0] = 34; for (int i = 0; i < 5; ++i) { System.out.println("Element index " + i +: " + age[i]); }; }; };
Output:
Element index 0: 34 Element index 1: 0 Element index 2: 14 Element index 3: 0 Element index 4: 0
The following program calculates the sum and average of values stored in an int type array.
class SumAverage { public static void main(String[] args) { int[] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12}; int sum = 0; Double average; //Used to access elements in each loop for (int number: numbers) { sum += number; }; int arrayLength = numbers.length; //Changing sum and array length to double because average is a double average = ((double)sum / (double)arrayLength); System.out.println("Sum = " + sum); System.out.println("Average = " + average); }; };
Output:
Sum = 36 Average = 3.6
In the above example, we created a named numbered array. We use the for...each loop to access each element of the array. For more information on the for...each loop, please visitJava for...each loop.
In the loop, we calculate the sum of each element. Note this line,
int arrayLength = number.length;
Here, we use the length property of the array to calculate the size of the array. Then, we use the following formula to calculate the average:
average = ((double)sum / (double)arrayLength);
As you can see, we are converting int values to double. In Java, this is called type casting. For more information on type casting, please visitJava Type Conversion.
The array we are talking about is called a one-dimensional array. However, we can declare multidimensional arrays in Java.
Multidimensional arrays are arrays of arrays. That is, each element of a multidimensional array is an array itself. For example,
double[][] matrix = {{1.2, 4.3, 4.0}, {4.1, -1.1}; };
Here, we create a multidimensional array called matrix. It is a two-dimensional array. For more information, please visitJava Multidimensional Arrays.