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 then0, forStringnull)
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(); }}
Input5 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. Withi <= numbers.lengththe 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, useArrays.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]); // 99Array 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 anArrayList(see lists and maps in Java).
Tasks on this topic
Practise what you learned right in your browser. Tests check your solution instantly.
Related articles
- LoopsJava loops explained simply: for, while, do-while, break and continue with examples. Plus the common mistakes that cost points in exams.Read article
- Lists and mapsLists and maps in Java explained simply: ArrayList, HashSet and HashMap with examples. Plus common mistakes like List<int> and remove on numbers.Read article
- SearchingLinear and binary search in Java explained simply: with code examples, O(n) and O(log n) running times and the typical mistakes made in exams.Read article