Tutorial · 589 words · 3 minute read

Java Arrays — Complete Tutorial with 15 Practice Problems

By · Published 2026-02-25 · Updated 2026-08-24

An array is a fixed-length Java object whose indexed slots all have one component type. Arrays are useful when the size is known or when compact indexed access matters. The core skill is not declaration syntax; it is maintaining a correct index range and a useful invariant while traversing.

Declaration, defaults, and bounds

int[] scores = new int[3];       // {0, 0, 0}
int[] primes = {2, 3, 5, 7};
String[] names = new String[2]; // {null, null}

scores.length is 3, so valid indices are 0, 1, and 2. Length cannot change after construction. The variable can later refer to a different array unless the reference is final. Primitive arrays receive primitive defaults; reference arrays receive null references.

Learning order

  1. Creation, defaults, length, indexed traversal, and enhanced traversal.
  2. Single-pass sum, count, minimum, maximum, and linear search.
  3. In-place reversal and partitioning with two pointers.
  4. Copying, sorting, binary search, and the guarantees of java.util.Arrays.
  5. Prefix sums and sliding windows for contiguous ranges.
  6. Nested traversal of rectangular and jagged two-dimensional arrays.

Choose the loop from the operation

Use an enhanced for loop when only values matter. Use an indexed loop when you need the position, a neighboring element, or in-place modification. A for-each variable is a copy of the element value or reference; assigning to that variable does not replace the slot.

Tested example: maximum and index

public class Main {
    static int indexOfMaximum(int[] values) {
        if (values.length == 0) throw new IllegalArgumentException("empty array");
        int best = 0;
        for (int i = 1; i < values.length; i++) {
            if (values[i] > values[best]) best = i;
        }
        return best;
    }

    public static void main(String[] args) {
        int[] values = {-4, 9, 9, 2};
        int index = indexOfMaximum(values);
        System.out.println(index + ": " + values[index]);
    }
}

The output is 1: 9. The method rejects an empty array and deliberately returns the first maximum because it updates only for a strictly greater value. It runs in O(n) time and O(1) extra space.

Copy, alias, and mutate

int[] b = a copies a reference, so mutations through either variable affect the same array. Use Arrays.copyOf, clone, or System.arraycopy for a shallow copy. In a two-dimensional array, a shallow outer copy still shares the row arrays.

Two-dimensional and jagged arrays

int[][] rows = {{1, 2}, {3}, {4, 5, 6}};
for (int row = 0; row < rows.length; row++) {
    for (int column = 0; column < rows[row].length; column++) {
        System.out.println(rows[row][column]);
    }
}

Use each row’s length. Java represents a 2D array as an array of array references, so rows can have different lengths or even be null.

Common mistakes

  • Using i <= values.length and reading one position past the end.
  • Initializing a maximum to zero when every input may be negative.
  • Sorting an array even though the caller needs the original order.
  • Comparing arrays with equals instead of Arrays.equals, or nested arrays without Arrays.deepEquals.
  • Using binary search without first ensuring the searched range is sorted according to the same ordering.

15 practice problems in order

  1. Declare and initialize an array
  2. Calculate an array sum
  3. Find the maximum
  4. Calculate the average
  5. Count even values
  6. Implement linear search
  7. Reverse in place
  8. Find the second largest distinct value
  9. Remove duplicates
  10. Rotate an array
  11. Merge sorted arrays
  12. Implement binary search
  13. Find a pair with a target sum
  14. Find a maximum subarray sum
  15. Find an equilibrium point

If a link is harder than expected, return to the array topic guide or experiment with a four-element input in the array compiler examples. For each problem, state whether you mutate the input and what empty input means before coding.

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