Lesson Completion
Back to course

Arrays Class Utility Methods

Beginner
15 minutes4.7Java

1. The Hook (The "Byte-Sized" Intro)

In a Nutshell: The java.util.Arrays class provides utility methods for common array operations: sorting, searching, comparing, copying, and converting. One import, many superpowers!

When LinkedIn displays "Skills match": Arrays.equals(yourSkills, jobSkills). Built-in comparison!


2. Arrays Class Methods Overview

graph TB Arrays["java.util.Arrays"] --> Sort["sort()<br/>parallelSort()"] Arrays --> Search["binarySearch()"] Arrays --> Compare["equals()<br/>deepEquals()<br/>compare()"] Arrays --> Copy["copyOf()<br/>copyOfRange()"] Arrays --> Fill["fill()<br/>setAll()"] Arrays --> Convert["toString()<br/>asList()"]

3. Interactive & Applied Code

java
import java.util.Arrays; import java.util.List; public class ArraysClassDemo { public static void main(String[] args) { int[] nums = {5, 2, 8, 1, 9}; // === SORTING === Arrays.sort(nums); System.out.println(Arrays.toString(nums)); // [1, 2, 5, 8, 9] // === BINARY SEARCH (must be sorted!) === int index = Arrays.binarySearch(nums, 5); System.out.println("Index of 5: " + index); // 2 // === COPYING === int[] copy1 = Arrays.copyOf(nums, nums.length); // Full copy int[] copy2 = Arrays.copyOf(nums, 10); // Extend to 10 int[] copy3 = Arrays.copyOfRange(nums, 1, 4); // [1] to [3] // === COMPARING === int[] a = {1, 2, 3}; int[] b = {1, 2, 3}; int[] c = {1, 2, 4}; System.out.println(Arrays.equals(a, b)); // true System.out.println(Arrays.equals(a, c)); // false // Deep comparison for nested arrays int[][] matrix1 = {{1, 2}, {3, 4}}; int[][] matrix2 = {{1, 2}, {3, 4}}; System.out.println(Arrays.deepEquals(matrix1, matrix2)); // true // === FILLING === int[] filled = new int[5]; Arrays.fill(filled, 42); System.out.println(Arrays.toString(filled)); // [42, 42, 42, 42, 42] // Fill range Arrays.fill(filled, 1, 4, 0); // Fill index 1-3 with 0 System.out.println(Arrays.toString(filled)); // [42, 0, 0, 0, 42] // === TO STRING === System.out.println(Arrays.toString(nums)); // [1, 2, 5, 8, 9] // Deep toString for 2D int[][] matrix = {{1, 2}, {3, 4}}; System.out.println(Arrays.deepToString(matrix)); // [[1, 2], [3, 4]] // === AS LIST (fixed-size!) === String[] names = {"Alice", "Bob", "Charlie"}; List<String> list = Arrays.asList(names); System.out.println(list); // [Alice, Bob, Charlie] // list.add("Dave"); // ❌ UnsupportedOperationException! // === COMPARE (Java 9+) === int result = Arrays.compare(a, c); // -1 (a < c) // === MISMATCH (Java 9+) === int mismatchIdx = Arrays.mismatch(a, c); // 2 (first difference) // === PARALLEL OPERATIONS === int[] large = new int[1000000]; Arrays.parallelSort(large); // Multi-threaded sort Arrays.parallelSetAll(large, i -> i * 2); // Parallel init } }

⚠️ Common Mistakes

Mistake #1: asList() returns fixed-size list

java
List<String> list = Arrays.asList("a", "b"); list.add("c"); // ❌ UnsupportedOperationException! // Fix: new ArrayList<>(Arrays.asList(...))

Mistake #2: binarySearch on unsorted array

java
int[] arr = {5, 2, 8}; Arrays.binarySearch(arr, 5); // ❌ Undefined result! // Always sort first

Mistake #3: Using toString() on 2D arrays

java
int[][] matrix = {{1,2},{3,4}}; System.out.println(Arrays.toString(matrix)); // Addresses! // Use: Arrays.deepToString(matrix)

4. The "Interview Corner"

🏆 Q1: "Difference between copyOf() and clone()?" Answer: copyOf() creates new array of specified size (can extend/truncate). clone() creates exact shallow copy.

🏆 Q2: "Why use deepEquals() for 2D arrays?" Answer: equals() compares row references, not contents. deepEquals() recursively compares nested elements.


🎓 Key Takeaways

sort() / binarySearch() for ordering/finding
copyOf() / copyOfRange() for copying
equals() / deepEquals() for comparing
toString() / deepToString() for printing
asList() → fixed-size list (wrap in ArrayList for modification)

Topics Covered

Java FundamentalsArrays

Tags

#java#arrays#data-structures#multidimensional-arrays#beginner-friendly

Last Updated

2025-02-01