Lab 04: Arrays & Multi-Dimensional Arrays

Objective

Declare, initialize, and manipulate single and multi-dimensional arrays in Java, use Arrays utility methods, and understand the difference between arrays and references.

Background

Arrays are Java's fundamental data structure — a fixed-size, ordered collection of elements of the same type. Unlike JavaScript arrays, Java arrays have a fixed length set at creation time. Understanding arrays is prerequisite to collections, matrices, and algorithm implementation.

Time

35 minutes

Prerequisites

  • Lab 02 (Variables & Primitives)

  • Lab 03 (Strings)

Tools

  • Java 21 (Eclipse Temurin)

  • Docker image: innozverse-java:latest


Lab Instructions

Step 1: Declaring and Initializing Arrays

💡 Arrays in Java are objectsscores.length (no parentheses) is a field, not a method. Array indices are zero-based. Accessing out-of-bounds throws ArrayIndexOutOfBoundsException.

📸 Verified Output:


Step 2: Iterating Arrays

💡 Arrays.binarySearch requires a sorted array. Always sort first. It returns the index if found, or a negative value if not. Use Arrays.sort() which uses dual-pivot quicksort — O(n log n).

📸 Verified Output:


Step 3: Array Copying

💡 Arrays are reference types — assigning an array variable copies the reference, not the data. int[] b = a means both variables point to the same memory. Always use .clone(), Arrays.copyOf(), or System.arraycopy() for true copies.

📸 Verified Output:


Step 4: 2D Arrays — Matrices

💡 Java 2D arrays are arrays of arraysint[][] is literally an int[] whose elements are int[] objects. This allows jagged arrays (rows of different lengths), which are common in triangle problems, Pascal's triangle, and adjacency lists.

📸 Verified Output:


Step 5: Common Array Algorithms

💡 The rotate trick (reverse all, reverse first k, reverse rest) runs in O(n) time and O(1) space — no extra array needed. This pattern — reversing sub-arrays to achieve rotation — is a classic interview technique worth memorizing.

📸 Verified Output:


Step 6: Arrays as Method Parameters & varargs

💡 Varargs int... numbers is syntactic sugar for int[] numbers — Java creates the array automatically. Under the hood sum(1, 2, 3) becomes sum(new int[]{1, 2, 3}). You can pass an explicit array too. Varargs must be the last parameter.

📸 Verified Output:


Step 7: Sorting with Comparators

💡 Comparator.comparing chains (thenComparing) make multi-key sorts readable. The record type (Java 16+) auto-generates constructor, accessors, equals, hashCode, and toString — perfect for data carriers.

📸 Verified Output:


Step 8: Array Utilities Wrap-up

💡 Arrays.stream(arr) bridges arrays and the Streams API, giving you filter, map, reduce, sum, average, and more. Arrays.parallelSort() uses multiple CPU cores for large arrays — measurably faster than Arrays.sort() above ~10,000 elements.

📸 Verified Output:


Verification

Expected: All outputs match verified output above.

Summary

Arrays are Java's foundation for data storage. You've covered declaration, initialization, iteration, copying (reference vs value), 2D/jagged arrays, sorting with comparators, varargs, and bridging to Streams. Next stop: Collections, which solve the fixed-size limitation.

Further Reading

Last updated