How To Use Array in Java

Data Structure

By Mayuri Fakirpure

Updated on Feb 10, 2024

In this chapter, we'll explain how to use Array in Java.

Array is a data structure, it allows us to store multiple values in single variable with same data type. Arrays in java are fixed in size. Once we define the size of an array, it cannot able to change during runtime. We can access particular element of an array using index notation.

It has benefits like it makes the code optimized, we can retrieve or sort the data efficiently. We can get any data located at an index position.

Here is a basic explanation of arrays in Java with an example.

Declaring an Array

Let's declare an array. First we need to define the type of elements with square brackets '[]' and assign it to the variable name.

// Declare an array of integers
int[] myArray;

Here int is type of elements. Meaning, the array will hold integer type of elements. myArray is a variable name.

Creating an Array

In previous step, we have declared the array. Now, let's create an array in Java. First, we'll see how the array gets created:

// Create an array of integers with size 5
myArray = new int[5];

We have used new keyword to create an array. Next, the type of elements the array will hold and the size of the array in square brackets []. The size defines how many data allows to store in the array. For this demonstration purpose we have set size 5, so we can store 5 integer values or elements in the array.

Alternatively, we can declare and create an array in a single line:

// Declare and create an array of integers with size 5
int[] myArray = new int[5];

Initializing an Array

We have created an array. Now, let's insert values in it.

// Initialize an array of integers with values
int[] myArray = {10, 20, 30, 40, 50};

To create an array of integers, we could write:

String[] lang = {"Java", "Python", "Golang", "PHP"};

Accessing Array Elements

Once we insert data in the array, we can access elements by index. Array starts from 0, so the index is like 0,1,2,3... and so on.

// Access the first element of the array
int firstElement = myArray[0]; // Value will be 1

Example Program

Here is a simple example of array in Java

Create a ArrayExample.java file and copy and paste following code:

public class ArrayExample {
    public static void main(String[] args) {

        // Initialize array elements with values
        int[] myArray = {10, 20, 30, 40, 50};

        // Access and print array elements
        System.out.println("Array elements:");
        for (int i = 0; i < myArray.length; i++) {
            System.out.println("Element at index " + i + ": " + myArray[i]);
        }
    }
}

Note: Here we are using integer example. For String replace int[] array with String[]

Output

Array elements:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50

We can declare array size and assign the values. For example:

Create a ArrayExample.java file and copy and paste following code:

public class ArrayExample {
    public static void main(String[] args) {
        // Declare and create an array of integers with size 5
        int[] myArray = new int[5];

        // Initialize array elements with values
        myArray[0] = 10;
        myArray[1] = 20;
        myArray[2] = 30;
        myArray[3] = 40;
        myArray[4] = 50;

        // Access and print array elements
        System.out.println("Array elements:");
        for (int i = 0; i < myArray.length; i++) {
            System.out.println("Element at index " + i + ": " + myArray[i]);
        }
    }
}

Output

Array elements:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50

In this chapter we have seen how to use array in Java with example creates an integer array, initializers its elements with values, and prints out each element using for loop. This chapter is completed.