Java Reference
In-Depth Information
How It Works
We have defined the regular expression so that separate capturing groups identify the method name and
both arguments. As we saw earlier, the method name corresponds to group 1, the first argument to
group 2, and the second argument to group 8. We therefore define the replacement string to the
appendReplacement()
method as "
$1\\($8,$2\\)
". The effect of this is to replace the text for
each method call that is matched by the following, in sequence:
$1
The text matching capturing group 1, which will be the method name.
\\(
A left parenthesis.
$8
The text matching capturing group 8, which will be the second argument.
,
A comma.
$2
The text matching capturing group 2, which will be the first argument.
\\)
A right parenthesis.
The call to
appendTail()
is necessary to ensure that any text left at the end of
oldCode
following
the last match for
regEx
gets copied to
newCode
.
In the process we have eliminated any superfluous whitespace that was laying around in the original text.
Summary
Regular expressions are a very powerful capability that we only touched on in this chapter.
The important elements we've covered are:
The
java.util.Arrays
class provides static methods for sorting, searching, filling, and
comparing arrays.
❑
Objects of type
Random
can generate pseudo-random numbers of type
int
,
long
,
float
,
and
double
. The integers are uniformly distributed across the range of the type
int
or
long
.
The floating point numbers are between 0.0 and 1.0. You can also generate numbers of type
double
with a Gaussian distribution with a mean of 0.0 and a standard deviation of 1.0, and
random
boolean
values.
❑
Classes derived from the
Observable
class can signal changes to classes that implement the
Observer
interface. You define the
Observer
objects that are to be associated with an
Observable
class object by calling the
addObserver()
method. This is primarily intended to
be used to implement the document/view architecture for applications in a GUI environment.
❑
You can create
Date
objects to represent a date and time that you specify in milliseconds
since January 1, 1970, 00:00:00 GMT, or the current date and time from your computer clock.
❑
You can use a
DateFormat
object to format the date and time for a
Date
object as a string.
The format will be determined by the style and the locale that you specify.
❑