Arrays in Java
Learn how arrays store multiple values of the same type, how to create and access them, and how to work with multidimensional arrays and common array operations.
What is an Array?
An array is a fixed-size data structure that stores multiple values of the same type in a contiguous logical sequence.
Each element is accessed using an index. Java array indexes
start from 0.
int[] numbers = {10, 20, 30, 40, 50};
Declaring an Array
Java supports two common declaration styles.
int[] numbers;
String[] names;
The preferred style places the brackets with the type because it clearly communicates that the variable is an array.
Creating an Array
The new keyword creates an array with a fixed
length.
int[] numbers = new int[5];
This creates an integer array capable of storing five values.
Array Initialization
An array can be initialized at the time of declaration.
int[] numbers = {10, 20, 30, 40, 50};
String[] languages = {
"Java",
"C#",
"Python"
};
Accessing Array Elements
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[0]);
System.out.println(numbers[2]);
System.out.println(numbers[4]);
10
30
50
Modifying Array Elements
Array elements can be changed using their index.
int[] numbers = {10, 20, 30};
numbers[1] = 100;
System.out.println(numbers[1]);
100
Array Length
The length field returns the number of elements
in an array.
int[] numbers = {10, 20, 30, 40};
System.out.println(numbers.length);
4
Traversing an Array with for Loop
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
Enhanced for Loop
The enhanced for loop provides a simple way to
iterate through every element.
int[] numbers = {10, 20, 30, 40, 50};
for (int number : numbers) {
System.out.println(number);
}
Default Values
When an array is created using new, its elements
receive default values according to their type.
| Type | Default Value |
|---|---|
| int | 0 |
| double | 0.0 |
| boolean | false |
| char | \u0000 |
| Reference types | null |
Array Index
For an array with length n, valid indexes range
from 0 to n - 1.
int[] numbers = {10, 20, 30};
numbers[0] // valid
numbers[1] // valid
numbers[2] // valid
numbers[3] // invalid
ArrayIndexOutOfBoundsException.
Multidimensional Arrays
Java supports arrays of arrays. A two-dimensional array is commonly used to represent rows and columns.
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Accessing Multidimensional Arrays
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(matrix[0][0]);
System.out.println(matrix[1][2]);
System.out.println(matrix[2][1]);
1
6
8
Traversing a Two-Dimensional Array
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(
matrix[i][j] + " "
);
}
System.out.println();
}
1 2 3
4 5 6
7 8 9
Jagged Arrays
Java does not require every row of a multidimensional array to have the same length.
int[][] numbers = {
{1, 2},
{3, 4, 5},
{6}
};
System.out.println(numbers[0].length);
System.out.println(numbers[1].length);
System.out.println(numbers[2].length);
2
3
1
java.util.Arrays
The java.util.Arrays utility class provides
convenient methods for working with arrays.
import java.util.Arrays;
int[] numbers = {5, 2, 8, 1, 3};
Arrays.sort(numbers);
System.out.println(
Arrays.toString(numbers)
);
[1, 2, 3, 5, 8]
Copying Arrays
The Arrays.copyOf() method can create a new array
containing copied elements.
import java.util.Arrays;
int[] original = {10, 20, 30};
int[] copy = Arrays.copyOf(
original,
original.length
);
System.out.println(
Arrays.toString(copy)
);
Searching an Array
A sorted array can be searched using
Arrays.binarySearch().
import java.util.Arrays;
int[] numbers = {10, 20, 30, 40, 50};
int index = Arrays.binarySearch(
numbers,
30
);
System.out.println(index);
binarySearch() should be used with an array sorted
according to the method's expected ordering.
Filling an Array
import java.util.Arrays;
int[] numbers = new int[5];
Arrays.fill(numbers, 10);
System.out.println(
Arrays.toString(numbers)
);
[10, 10, 10, 10, 10]
Arrays of Objects
Arrays can store references to objects.
class Student {
String name;
Student(String name) {
this.name = name;
}
}
Student[] students = {
new Student("Amit"),
new Student("Neha"),
new Student("Rahul")
};
for (Student student : students) {
System.out.println(student.name);
}
Array vs Collection
| Feature | Array | Collection |
|---|---|---|
| Size | Fixed | Usually dynamic |
| Primitive Values | Supported | Use wrapper types |
| Built-in Methods | Limited | Rich API |
| Performance | Simple and efficient | Depends on implementation |
| Use Case | Known fixed-size data | Dynamic data structures |
Real-World Example: Student Marks
int[] marks = {
78,
85,
92,
67,
88
};
int total = 0;
for (int mark : marks) {
total += mark;
}
double average =
(double) total / marks.length;
System.out.println(
"Total: " + total
);
System.out.println(
"Average: " + average
);
Advantages of Arrays
- Simple and efficient indexed access.
- Stores multiple values under one variable.
- Supports primitive values directly.
- Useful when the required size is known in advance.
Limitations of Arrays
- Array size cannot be changed after creation.
- Insertion and deletion operations are not as convenient as collection APIs.
- More complex data-management requirements may be better handled using collections.
Array Best Practices
-
Use
array.lengthinstead of hard-coded lengths when iterating. -
Prefer enhanced
forloops when the index is not required. -
Use
java.util.Arraysfor common utility operations. - Validate indexes when values come from external input.
-
Use collections such as
ArrayListwhen dynamic sizing is required.
Interview Questions
0.
ArrayIndexOutOfBoundsException.
length field, while
String uses the
length() method.
Summary
Java arrays provide fixed-size indexed storage for values
of the same type. They support one-dimensional and
multidimensional structures, direct indexed access,
iteration, and utility operations through
java.util.Arrays.