Master Core Java Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

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.

Java
int[] numbers = {10, 20, 30, 40, 50};

Declaring an Array

Java supports two common declaration styles.

Java
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.

Java
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.

Java
int[] numbers = {10, 20, 30, 40, 50};

String[] languages = {
    "Java",
    "C#",
    "Python"
};

Accessing Array Elements

Java
int[] numbers = {10, 20, 30, 40, 50};

System.out.println(numbers[0]);

System.out.println(numbers[2]);

System.out.println(numbers[4]);
Output
10
30
50

Modifying Array Elements

Array elements can be changed using their index.

Java
int[] numbers = {10, 20, 30};

numbers[1] = 100;

System.out.println(numbers[1]);
Output
100

Array Length

The length field returns the number of elements in an array.

Java
int[] numbers = {10, 20, 30, 40};

System.out.println(numbers.length);
Output
4

Traversing an Array with for Loop

Java
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.

Java
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.

Java
int[] numbers = {10, 20, 30};

numbers[0]   // valid
numbers[1]   // valid
numbers[2]   // valid

numbers[3]   // invalid
Important: Accessing an invalid index causes ArrayIndexOutOfBoundsException.

Multidimensional Arrays

Java supports arrays of arrays. A two-dimensional array is commonly used to represent rows and columns.

Java
int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

Accessing Multidimensional Arrays

Java
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]);
Output
1
6
8

Traversing a Two-Dimensional Array

Java
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();

}
Output
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.

Java
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);
Output
2
3
1

java.util.Arrays

The java.util.Arrays utility class provides convenient methods for working with arrays.

Java
import java.util.Arrays;

int[] numbers = {5, 2, 8, 1, 3};

Arrays.sort(numbers);

System.out.println(
    Arrays.toString(numbers)
);
Output
[1, 2, 3, 5, 8]

Copying Arrays

The Arrays.copyOf() method can create a new array containing copied elements.

Java
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().

Java
import java.util.Arrays;

int[] numbers = {10, 20, 30, 40, 50};

int index = Arrays.binarySearch(
    numbers,
    30
);

System.out.println(index);
Note: binarySearch() should be used with an array sorted according to the method's expected ordering.

Filling an Array

Java
import java.util.Arrays;

int[] numbers = new int[5];

Arrays.fill(numbers, 10);

System.out.println(
    Arrays.toString(numbers)
);
Output
[10, 10, 10, 10, 10]

Arrays of Objects

Arrays can store references to objects.

Java
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

Java
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.length instead of hard-coded lengths when iterating.
  • Prefer enhanced for loops when the index is not required.
  • Use java.util.Arrays for common utility operations.
  • Validate indexes when values come from external input.
  • Use collections such as ArrayList when dynamic sizing is required.

Interview Questions

An array is a fixed-size object that stores multiple values of the same array component type and provides indexed access.

Java arrays use zero-based indexing, so the first element is at index 0.

No. An array has a fixed length after it is created. A new array must be created if a different length is required.

Java throws an ArrayIndexOutOfBoundsException.

Arrays use the 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.