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.
1 |
int[] intarray; |
or
1 |
int intarray[]; |
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.
1 |
intarray = new int[10] |
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.
1 |
int[] intarray = new int[10]; |
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.
1 |
int[] intarray = new int[]{1, 2, 3}; |
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.
1 |
int[] intarray = {1, 2, 3}; |
How to get the size of an array?
The length
property of the array returns the number of elements stored in an array.
1 2 3 4 5 6 7 8 |
int[] intArray = new int[5]; /* * To get the size of an array, use the * length property. */ //this will print 5 System.out.println("Size of an array: " + intArray.length ); |
Output
1 |
Size of an array: 5 |
How to get elements from an array?
Once the array is created, its elements can be accessed using an array index.
Example:
1 2 3 4 |
int[] intarray = new int[]{1, 2, 3}; //access array elements using the index System.out.println( intarray[0] ); |
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?
1 2 3 4 5 6 7 |
int[] intarray = {1, 2, 3}; int i=0; while(i < intarray.length){ System.out.println( intarray[i] ); i++; } |
Output
1 2 3 |
1 2 3 |
How to iterate array elements using the do while loop?
1 2 3 4 5 6 7 8 9 |
int[] intarray = {4, 5, 6}; if(intarray.length > 0){ int i = 0; do{ System.out.println( intarray[i] ); i++; }while(i < intarray.length); } |
Output
1 2 3 |
4 5 6 |
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?
1 2 3 4 |
int[] intarray = {7,8}; for(int i =0 ; i < intarray.length; i++){ System.out.println( intarray[i] ); } |
Output
1 2 |
7 8 |
How to iterate array elements using the enhanced for loop?
1 2 3 4 |
int[] intarray = {9, 10, 11}; for( int i : intarray ){ System.out.println( i ); } |
Output
1 2 3 |
9 10 11 |
Below given Java array examples will help you understand the important concepts of the array in more detail.
Java Array Examples
Array Basic Examples
- Get array length example
- How to print array elements in Java
- How to define and use Java String array
- How to find element index in array like indexOf method
- Join two arrays
- Reverse array example
- How to create ArrayList containing arrays
- How to add arrays to ArrayList
- How to initialize String array
- Remove duplicate elements from String array
- How to find duplicate elements in an int array
- How to check if array contains value or element
- Sort String[] array example
- Sort String[] array containing numbers
- How to add an array to LinkedList
Array Conversions Examples
- Convert array to ArrayList
- Convert ArrayList to array
- Convert ArrayList to String array
- Convert array to HashSet
- Convert HashSet to array
- Convert int array to String
- Convert byte array to String
- Convert String to byte array
- Convert String to String[] array
- Convert String[] array to String
- Convert List to array
- Convert String to character array or char array
- Convert String array to ArrayList
- Convert LinkedList to Array
- Convert Array to LinkedList
- Convert array to Vector
- Convert Vector to array
- Convert regex matches to an array
References:
Java Array Tutorial Javadoc
Please let me know if you liked the Java array tutorial with examples in the comments section below.