Declare, initialize, and manipulate single and multi-dimensional arrays in Java, use Arrays utility methods, and understand the difference between arrays and references.
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.
35 minutes
Lab 02 (Variables & Primitives)
Java 21 (Eclipse Temurin)
Docker image: innozverse-java:latest
Lab Instructions
Step 1: Declaring and Initializing Arrays
💡 Arrays in Java are objects — scores.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 arrays — int[][] 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:
Expected: All outputs match verified output above.
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