Chapter 15
A Collection of Useful Classes
WHAT YOU WILL LEARN IN THIS CHAPTER
- How to use the static methods in the Arrays class for filling, copying, comparing, sorting, and searching arrays
- How to use the Observable class and the Observer interface to communicate between objects
- What facilities the Random class provides
- How to create and use Date and Calendar objects
- What regular expressions are and how you can create and use them
- What a Scanner class does and how you use it
In this chapter you look at some more useful classes in the java.util package, but this time they are not collection classes — just a collection of classes. You also look at the facilities provided by classes in the java.util.regex package that implement regular expressions in Java. Support for regular expressions is a very powerful and important feature of Java.
UTILITY METHODS FOR ARRAYS
The java.util.Arrays class defines a set of static methods for operating on arrays. You have methods for sorting and searching arrays, as well as methods for comparing arrays of elements of a basic type. You also have methods for filling arrays with a given value. Let’s look at the simplest method first, the fill() method for filling an array.
Filling an Array
The need to fill an array with a specific value arises quite often, and you already met the static fill() method that is defined in the Arrays class in Chapter 4. The fill() method comes in a number of overloaded versions of the form
fill(type[] array, type value) ...