Java Array

In Java, Array is a collection of items in which items are ordered. Array is immutable, meaning, we cannot add or remove elements from an array.

For example, an array of integers assigned to a variable arr is

int arr[] = {2, 4, 6};

An array of strings is

String arr[] = {"apple", "banana", "cherry"};

Java Array Tutorials

Before actually getting introduced to Arrays in Java, following are the list of tutorials that we cover about Arrays in Java.

Basics

Iterate Over Array

Checks

Transformations / Modifications

Example Programs

ADVERTISEMENT

Quick Introduction to Arrays

The syntax to declare an Array is

datatype arrayName[];

Please observe the placement of square brackets, right after the array name. We can also place the square brackets right after datatype as shown in the following.

datatype[] arrayName;

We can use any of these two notations.

So, to define an integer array, we would write a statement as shown below.

int numbers[];
//or
int[] numbers;

When you declare an array, we are only reserving the variable name and notifying the compiler that we are going to use this variable name to store elements of this particular datatype.

To initialize an array, we can assign the array variable with new array of specific size as shown below.

arrayName = new datatype[size];

We have to mention the size of array during initialization. This will create an array in memory, with all elements initialized to their corresponding static default value.

In the following program, we initialized an integer array with a size of 10.

Main.java

public class Main {
    public static void main(String[] args) {
        int numbers[];
        numbers = new int[10];
    }
}

We have first declared the variable and then initialized it. Of course, we have declared and initialized the array in two different statements. But you can combine the declaration and initialization, to form the definition of array, as shown below.

Main.java

public class Main {
    public static void main(String[] args) {
        int numbers[] = new int[10];
    }
}

In the above example, we have created an integer array named numbers, and initialized it to an integer array of size 10 with default values. The default value for an integer is 0.

You can also assign elements directly to the array when declaring it.

In the following example, we have declared and initialized array with elements.

Main.java

public class Main {
    public static void main(String[] args) {
        int[] numbers = {2, 4, 6, 8, 10};
    }
}

Now numbers is an integer array with size of 5, because there are five elements in the array we assigned.

The size of array is fixed. You can only modify the existing elements of the array. But you cannot remove elements or add elements to the array.

How to access Array Elements?

Now that we have initialized an array with elements, how can we access them? Remember, we mentioned during introduction that an array is stored in continuous memory locations. And these elements can be accessed using index. index is the position of the element from starting of the array. The first element has an index of 0, second element has an index of 1, third element has an index of 2 and so on.

Following is the syntax to access an element of array.

arrayName[index]

The above statement can be used either to read an element at given index, or set an element at given index. The read or write operation depends on which side of assignment operator you are placing this.

For example, in the following program, we are reading the element of array nums at index 4.

Main.java

public class Main {
    public static void main(String[] args) {
        int[] numbers = {2, 4, 6, 8, 10, 12, 14, 16};
        int n = numbers[4];
        System.out.println("Element at index=4 is : " + n);
    }
}

Output

Element at index=4 is : 10

And in the following example, we are updating the element of array at index 4.

Main.java

public class Main {
    public static void main(String[] args) {
        int[] numbers = {2, 4, 6, 8, 10, 12, 14, 16};
        numbers[4] = 25;
        int n = numbers[4];
        System.out.println("Element at index=4 is : " + n);
    }
}

Output

Element at index=4 is : 25

From all the above discussion, we now know that array is a collection of elements stored in a continuous memory. And we can use index to access the elements.

Now, how do we access elements of an array one by one in a loop? Well! Java has looping statements like while loop, for loop and advanced for loop. We can use any of these looping statements to iterate over elements of a Java Array.

In the following example, we have initialize a Java Array, and iterate over elements of Array using Java For Loop.

Main.java

public class Main {
    public static void main(String[] args) {
        int[] numbers = {2, 4, 6, 8, 10};
        for (int index=0; index < numbers.length; index++) {
            System.out.println(numbers[index]);
        }
    }
}

Output

2
4
6
8
10

Conclusion

In this Java Tutorial, we learned about Java Arrays and different concepts related to them with detailed tutorials and examples.