Java Arrays Explained: Indexes, Loops and 2D Arrays

Updated on October 22, 2026

An array in Java solves a simple problem: a variable stores only one value. For ten measurements or the seven days of the week, you'd need ten or seven variables. An array groups many values of the same type under one name, and a loop in Java lets you go through all of them.

Create, read, change

Square brackets after the type make it an array: int[], String[], double[]. There are two ways to create one:

  • with fixed values: String[] fruits = {"Apple", "Banana", "Cherry"};
  • with a size: int[] zeros = new int[4]; (all slots are then 0, for String null)

Every slot has a number, its index. Counting starts at 0: fruits[0] is the first element, fruits[fruits.length - 1] the last. length is the number of slots. It's fixed when the array is created and never changes.

Arrays.toString(...) (with import java.util.Arrays;) turns an array into readable text.

import java.util.Arrays;
public class Main {    public static void main(String[] args) {        String[] fruits = {"Apple", "Banana", "Cherry"};        System.out.println(fruits[0]);        System.out.println("Length: " + fruits.length);
        fruits[1] = "Mango";        for (int i = 0; i < fruits.length; i++) {            System.out.println(i + ": " + fruits[i]);        }
        int[] zeros = new int[4];        System.out.println(Arrays.toString(zeros));        System.out.println(Arrays.toString(fruits));    }}

Arrays and loops

The classic form runs an index from 0 to length - 1. With it you can read, write and even go backwards. That's also how linear search works.

If you don't need the index, the for-each loop is shorter: for (int number : numbers) means “for each number in numbers”. It only reads. You can't change values in the array with it.

import java.util.Arrays;import java.util.Scanner;
public class Main {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int n = scanner.nextInt();        int[] numbers = new int[n];        for (int i = 0; i < n; i++) {            numbers[i] = scanner.nextInt();        }        System.out.println("Read: " + Arrays.toString(numbers));
        int even = 0;        for (int number : numbers) {            if (number % 2 == 0) {                even++;            }        }        System.out.println("Even numbers: " + even);
        System.out.print("Backwards:");        for (int i = numbers.length - 1; i >= 0; i--) {            System.out.print(" " + numbers[i]);        }        System.out.println();    }}
Input
5
3 8 1 6 4

The first number of the input says how many numbers follow.

Two-dimensional arrays

An array can itself contain arrays, for example for a table, a game board or a cinema hall. int[][] points is an array of rows, each row an int[]. points[1][2] is row 1, column 2 (both counted from 0). points.length is the number of rows, points[row].length the length of one row.

public class Main {    public static void main(String[] args) {        // One row per student, one column per exercise        int[][] points = {            {3, 5, 4},            {2, 5, 5},            {4, 1, 3},        };        for (int row = 0; row < points.length; row++) {            int total = 0;            for (int col = 0; col < points[row].length; col++) {                total += points[row][col];            }            System.out.println("Student " + (row + 1) + ": " + total + " points");        }        System.out.println("Second student, third exercise: " + points[1][2]);    }}

Common mistakes

  • Index outside the array

    The last element has the index length - 1. With i <= numbers.length the loop reaches one step too far, and the program crashes:

    ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

    The condition needs <, not <=.

  • Printing an array directly

    System.out.println(numbers) doesn't print the contents but something like [I@1b6d3586. For the contents, use Arrays.toString(numbers).

  • Assigning is not copying

    An array variable points to the array, it doesn't contain it. After int[] b = a; both point to the same array:

    int[] a = {1, 2, 3};int[] b = a;b[0] = 99;System.out.println(a[0]); // 99
  • Array created too small

    An array's size can't be changed afterwards. If you only learn the count from the input, read it first and create the array afterwards: int[] numbers = new int[n];. If the collection should grow while the program runs, use an ArrayList (see lists and maps in Java).

Tasks on this topic

Practise what you learned right in your browser. Tests check your solution instantly.