Skip to content

Java Array Tutorial with Examples

Java array tutorial with examples will help you understand how to use the array in an easy way. An array in Java is a collection of the same type of elements. Java array is an Object and represents a fixed number of elements. That means the size of an array cannot be changed once it is created.

When an array in Java is created, memory is allocated depending upon the size of the array and the type of elements the array is going to hold. Each individual item in an array is called an element and it must be accessed using the array index.

Array index starts from 0 and goes up to n-1 where n is the size of an array. That means the first element of an array is located at index 0 and the last element of an array is located at size - 1 index.

How to declare an array in Java?

You can declare an array using the type of its elements and the reference name as given below.

or

Please note that we do not specify the number of elements while declaring an array in Java. Also, declaring an array will not allocate the memory to it. The memory will be allocated to an array at the creation time, not at the declaration time.

How to create an array in Java?

Once declared, you can create an array using the new keyword and assign it to the declared reference.

The above statement creates an int array of size 10 and assigns it to the reference we declared previously. At this point, the memory is allocated for the array to hold 10 int elements.

You can do both, the declaration and creation of an array together in one line like given below.

How to initialize an array in Java?

The arrays in Java can be initialized with the element values at the creation time. We can declare, create and initialize the array with elements in one statement as given below.

This will create an int array with 3 elements 1, 2, and 3. Note that we cannot specify the size with the initialization. The following statement can also be used to create and initialize an array.

How to get the size of an array?

The length property of the array returns the number of elements stored in an array.

Output

How to get elements from an array?

Once the array is created, its elements can be accessed using an array index.

Example:

The above statement will print 1. Since the array index starts from 0, intarray[0] will return the first element of an array. Similarly, intarray[2] will return element 3, which is also the last element of our array.

In addition to directly accessing array elements using its index, you can also iterate array elements using while loop, do while loop, for loop, and enhanced for loop. Idea is to loop through the array from index 0, where the first element of the array is located, to the array’s length – 1 element i.e. the last element.

How to iterate array elements using the while loop?

Output

How to iterate array elements using the do while loop?

Output

Tip: While using the do while loop, always make sure to check if an array has at least one element. The body of the do while loop is executed first and then the condition is checked. If the array is empty, the loop body will try to access the first element of an empty array which results in ArrayIndexOutOfBoundsException.

How to iterate array elements using the for loop?

Output

How to iterate array elements using the enhanced for loop?

Output

Below given Java array examples will help you understand the important concepts of the array in more detail.

Java Array Examples

Array Basic Examples

Array Conversions Examples

References:
Java Array Tutorial Javadoc

Please let me know if you liked the Java array tutorial with examples in the comments section below.

About the author

Leave a Reply

Your email address will not be published.