Java Arrays
Complete guide to Java arrays with examples. Learn array declaration, initialization, accessing elements, and array operations in Java programming.
An array is a collection of similar types of data.
For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names.
String[] array = new String[100];Here, the above array cannot store more than 100 names. The number of values in a Java array is always fixed.
What is the index of the first element in a Java array?
1. How to declare an array in Java?
In Java, here is how we can declare an array.
dataType[] arrayName;dataType- it can be primitive data types likeint,char,double,byte, etc. or Java objectsarrayName- it is an identifier
For example,
double[] data;Here, data is an array that can hold values of type double.
But, how many elements can array this hold?
Good question! To define the number of elements that an array can hold, we have to allocate memory for the array in Java. For example,
// declare an array
double[] data;
// allocate memory
data = new double[10];Here, the array can store 10 elements. We can also say that the size or length of the array is 10.
In Java, we can declare and allocate the memory of an array in one single statement. For example,
double[] data = new double[10];If you declare an int array of size 5, what is the default value of its elements?
2. How to Initialize Arrays in Java?
In Java, we can initialize arrays during declaration. For example,
//declare and initialize and array
int[] age = {12, 4, 5, 2, 5};Here, we have created an array named age and initialized it with the values inside the curly brackets.
Note that we have not provided the size of the array. In this case, the Java compiler automatically specifies the size by counting the number of elements in the array (i.e. 5).
In the Java array, each memory location is associated with a number. The number is known as an array index. We can also initialize arrays in Java, using the index number. For example,
// declare an array
int[] age = new int[5];
// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;
..Note
Array indices always start from 0. That is, the first element of an array
is at index 0. If the size of an array is n, then the last element of
the array will be at index n-1.
Tip 💡: The most common error in Java? ArrayIndexOutOfBoundsException. It means you tried to access index 5 in an array of size 5 (remember, indices go 0 to 4!).
3. Array Operations
Copying Arrays
In Java, you can copy an array using System.arraycopy() or Arrays.copyOf().
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[] source = {1, 2, 3, 4, 5};
int[] destination = Arrays.copyOf(source, source.length);
System.out.println("Destination array: " + Arrays.toString(destination));
}
}Sorting Arrays
You can sort arrays using Arrays.sort().
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[] numbers = {5, 2, 9, 1, 6};
Arrays.sort(numbers);
System.out.println("Sorted array: " + Arrays.toString(numbers)); // [1, 2, 5, 6, 9]
}
}Key Takeaways
- Fixed Size: Once created, you cannot change the size of an array.
- Zero-Indexed: The first element is at index
0, not1. - Homogeneous: All elements must be of the same type (e.g., all
int).
Common Pitfalls
[!WARNING] Printing Arrays:
System.out.println(array)prints a memory address (like[I@1b6d3586), not the contents. UseArrays.toString(array)or a loop to print values.
[!WARNING] Bounds: Accessing
array[length]throws anArrayIndexOutOfBoundsException. The last valid index islength - 1.
Challenge
Challenge
Task:
Create an array of integers named 'numbers' with values 10, 20, 30, 40, 50. Print the third element (index 2).