Java
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Java
-
Section 4: Collections Framework
-
Section 5: Exception Handling
-
Section 6: Generics
-
Section 7: Functional Java
-
Section 8: Concurrency
-
Section 9: I/O and NIO
-
Section 10: JVM Internals
-
Section 11: Modern Java Features
-
Section 12: Build Tools and Project Structure
-
Section 13: Testing
-
Section 14: Databases and Persistence
-
Section 15: Networking
-
Section 16: Design and Best Practices
-
Section 17: Reflection and Annotations
-
Section 18: Logging and Diagnostics
-
Section 19: Date, Time, and Internationalization
-
Section 20: Java Platform Module System
-
Section 21: Security in Java
-
Section 22: Advanced Collections and Data Structures
-
Section 23: More Concurrency Patterns
-
Section 24: Compression, Files, and System Integration
-
Section 25: GUI Programming
-
Section 26: Practical Projects
-
Section 27: Data Structures and Algorithms
-
Section 28: Interview and Algorithm Practice
-
Section 29: JSON and Data Interchange
-
Section 30: More Concurrency Utilities
-
Section 31: More Collections and Streams Practice
-
Section 32: More File and System Programming
-
Section 33: Standard Library Deep Dive
-
Section 34: More Practice and Drills
-
Section 35: More Testing and Quality
-
Section 36: More Design Patterns and Architecture
-
Section 37: Career and Ecosystem
-
Section 38: More OOP and Architecture Practice
-
Section 39: More Enterprise Concepts
-
Section 40: Advanced JavaFX
-
Section 41: More Interview Practice
275: The Arrays Utility Class
If you've been working with arrays for a while, you've probably noticed that they are surprisingly "dumb." If you want to print one, you're stuck writing a for loop. If you want to sort one, you're looking at writing a sorting algorithm from scratch. It's tedious, and frankly, it's a waste of your time. That's why Java gives us the java.util.Arrays class.
It's a utility class, meaning it's just a collection of static methods that operate on arrays. You don't instantiate it; you just call the methods directly. Let's build a simple high-score tracker for a game to see how this actually works in practice.
Printing the leaderboard without the madness
Let's start with a raw array of scores. Usually, if you try to print an array directly, Java gives you that cryptic memory address—something like [I@15db9742—which is completely useless for debugging.
import java.util.Arrays;
public class ScoreTracker {
public static void main(String[] args) {
int[] scores = {450, 120, 890, 340, 710};
// Instead of a loop, we use toString()
System.out.println("Current Scores: " + Arrays.toString(scores));
}
}
Arrays.toString() turns the array into a human-readable string. It's a small thing, but it saves you a dozen lines of boilerplate every single time you're debugging.
Organizing the chaos with a sort
A high-score list isn't much use if it's random. We need these sorted. Now, I could write a bubble sort, but why would I? Arrays.sort() uses a highly optimized version of Dual-Pivot Quicksort under the hood. It's faster than anything you'll likely write by hand.
// Right after our print statement:
Arrays.sort(scores);
System.out.println("Sorted Scores: " + Arrays.toString(scores));
// Output: [120, 340, 450, 710, 890]
The binary search trap
Now, let's say we want to check if a specific score—let's say 710—exists in our list. This is where I'll show you a mistake I made early in my career. I tried to use Arrays.binarySearch() on a list that hadn't been sorted yet.
Here's the deal: binary search is incredibly fast, but it only works if the array is already sorted. If you call it on an unsorted array, Java won't throw an error; it will just return a negative number or the wrong index, leaving you wondering why your code is lying to you.
// This works because we called Arrays.sort() above
int index = Arrays.binarySearch(scores, 710);
System.out.println("Score 710 found at index: " + index);
// If I had done this BEFORE Arrays.sort(), index would be unpredictable.
// Always sort first, then search.
Beyond these, you'll often see Arrays.fill() used to initialize an array with a default value, or Arrays.equals() to compare two arrays (since == only checks if they are the exact same object in memory, not if they contain the same values). Once you get used to the Arrays utility class, you'll realize how much manual labor it removes from your day.
📋 Practical Task
Exercise: Building a Grade Median Finder
You are tasked with creating a small tool that finds the median grade in a classroom. To do this, you'll need to utilize the java.util.Arrays class.
Write a program that does the following:
- Creates an array of 7 unsorted integers representing student grades (e.g.,
{88, 72, 95, 60, 82, 91, 77}). - Uses
Arrays.sort()to organize the grades from lowest to highest. - Prints the sorted list to the console using
Arrays.toString(). - Prints the median grade (the middle element of the sorted array).
- Uses
Arrays.binarySearch()to check if a specific "perfect score" of 100 exists in the array, and prints whether it was found or not.
There are no comments for now.