Lesson 67 min read

Arrays

Line up your data in neat, numbered rows

What's an Array?

An array is like a row of mailboxes in an apartment building. Each mailbox has a number (starting at 0), and each one holds exactly one item. When you create an array, you decide two things: what type goes in the boxes, and how many boxes you need.

Once you set the size, it's locked in — this is what makes them a static array. You can't add more mailboxes later — that's a job for ArrayList (a dynamic array that resizes itself). But arrays are fast, simple, and everywhere in Java.

Creating & Using Arrays

public class Main {
public static void main(String[] args) {
// Way 1: Declare size, fill later
int[] scores = new int[5];
scores[0] = 95;
scores[1] = 87;
scores[2] = 92;
scores[3] = 78;
scores[4] = 88;
// Way 2: Declare and fill at once
String[] colors = {"Red", "Green", "Blue", "Yellow"};
// Access by index
System.out.println("First score: " + scores[0]);
System.out.println("Last color: " + colors[colors.length - 1]);
System.out.println("Number of colors: " + colors.length);
// Loop through with for-each
System.out.print("All scores: ");
for (int s : scores) {
System.out.print(s + " ");
}
System.out.println();
// Modify an element
colors[1] = "Lime";
System.out.println("Changed: " + colors[1]);
}
}
Output
First score: 95
Last color: Yellow
Number of colors: 4
All scores: 95 87 92 78 88 
Changed: Lime

Useful Array Tricks

Java's Arrays utility class (from java.util.Arrays) gives you handy shortcuts:

  • Arrays.toString() — prints the array nicely instead of a weird memory address.
  • Arrays.sort() — sorts the array in ascending order in-place.
  • Arrays.fill() — fills every slot with the same value.
  • Arrays.copyOf() — creates a copy of the array (optionally with a different size).

Arrays Utility Methods

import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] nums = {42, 17, 8, 95, 3, 61};
// toString — pretty print
System.out.println("Original: " + Arrays.toString(nums));
// sort — arranges in order
Arrays.sort(nums);
System.out.println("Sorted: " + Arrays.toString(nums));
// fill — set everything to the same value
int[] zeros = new int[5];
Arrays.fill(zeros, 7);
System.out.println("Filled: " + Arrays.toString(zeros));
// copyOf — make a copy
int[] copy = Arrays.copyOf(nums, nums.length);
copy[0] = 999;
System.out.println("Copy: " + Arrays.toString(copy));
System.out.println("Original unchanged: " + Arrays.toString(nums));
}
}
Output
Original: [42, 17, 8, 95, 3, 61]
Sorted: [3, 8, 17, 42, 61, 95]
Filled: [7, 7, 7, 7, 7]
Copy: [999, 8, 17, 42, 61, 95]
Original unchanged: [3, 8, 17, 42, 61, 95]

Multidimensional Arrays — Grids & Tables

public class Main {
public static void main(String[] args) {
// 2D array — like a chessboard or spreadsheet
int[][] grid = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Access: grid[row][column]
System.out.println("Center: " + grid[1][1]); // 5
System.out.println("Bottom-right: " + grid[2][2]); // 9
// Print the whole grid
System.out.println("Full grid:");
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
System.out.print(grid[row][col] + " ");
}
System.out.println();
}
System.out.println("Rows: " + grid.length);
System.out.println("Cols: " + grid[0].length);
}
}
Output
Center: 5
Bottom-right: 9
Full grid:
1 2 3 
4 5 6 
7 8 9 
Rows: 3
Cols: 3
Note: Array indexes start at 0, not 1. An array of size 5 has indexes 0 through 4. Trying to access index 5 gives you an ArrayIndexOutOfBoundsException — Java's way of saying "that mailbox doesn't exist!" This is the most common array bug.

Quick check

What is the index of the last element in an array of size 10?

Continue reading

Dynamic ArraysData Structure
Resizing strategy, amortized O(1) push
Static ArraysData Structure
A row of numbered boxes — fast to grab, fixed in size
Lists & TuplesPython
Ordered collections you can grow, shrink, and slice
ArraysJavascript
The Swiss Army knife of JavaScript collections
LoopsMethods