Skip to Content
Course content

275: The Arrays Utility Class

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.