Tutorial · 8 minute read

Java Arrays — Complete Tutorial with 15 Practice Problems

By Java Practice Lab · Published 2026-02-25

Arrays are the foundation of every data structure in Java. This tutorial gives you everything you need — then sends you straight to practice problems.

Declaration & Initialization

int[] nums = new int[5];          // all zeros
int[] primes = {2, 3, 5, 7, 11};  // literal
String[] names = new String[3];   // all null

Traversal

Use a classic for-loop when you need the index, enhanced for-each when you only need values.

Common Algorithms

  • Find max / min — single pass O(n)
  • Reverse in place — two pointers O(n)
  • Rotate by k — reverse trick O(n)
  • Find duplicates — HashSet O(n)
  • Kadane's algorithm (max subarray) — O(n)

2D Arrays

int[][] grid = new int[3][4];
for (int i = 0; i < grid.length; i++) {
  for (int j = 0; j < grid[i].length; j++) {
    grid[i][j] = i * j;
  }
}

Arrays vs ArrayList

Arrays = fixed size, faster, primitives allowed. ArrayList = dynamic, only objects, more methods.

👉 Ready? Solve 15 array problems now.

Continue with the Java practice path · Try code in the Java compiler