Java Reference
In-Depth Information
Notice that the two files have about the same number of unique words and that
about half of the unique words appear in both files. Here is a second execution that
compares the text of Herman Melville's Moby-Dick to the text of Hamlet:
This program compares two text files
and reports the number of words in
common and the percent overlap.
file #1 name? moby.txt
file #2 name? hamlet.txt
file #1 words = 17305
file #2 words = 4874
common words = 3079
% of file 1 in overlap = 17.792545507078877
% of file 2 in overlap = 63.17193270414444
In this case, it is obvious that Moby-Dick has a much larger vocabulary. As a
result, only a small fraction of the words from Moby-Dick appear in Hamlet but a
large proportion of the words from Hamlet appear in Moby-Dick. It is well known
that Melville admired Shakespeare, so it is not surprising that his novel has a high
overlap with one of Shakespeare's plays.
As we mentioned in Chapter 6, you can obtain classic texts like these from the
Project Gutenberg web site at http://www.gutenberg.org.
Chapter Summary
The ArrayList class in Java's java.util package rep-
resents a growable list of objects implemented using an
array. You can use an ArrayList to store objects in a
sequential order. Each element has a zero-based index.
ArrayList s can be searched using methods named
contains , indexOf , and lastIndexOf .
Java's for-each loop can be used to examine each element
of an ArrayList . The list cannot be modified during the
execution of the for-each loop.
ArrayList is a generic class. A generic class accepts a
data type as a parameter when it is created, like
ArrayList<String> .
When you are storing primitive values such as int s or
double s into an ArrayList , you must declare the list with
special wrapper types such as Integer and Double .
An ArrayList maintains its own size for you; elements
can be added to or removed from any position up to the
size of the list. Other ArrayList operations include get ,
set , clear , and toString .
The Comparable interface defines a natural ordering for
the objects of a class. Objects that implement
Comparable can be placed into an ArrayList and
 
 
Search WWH ::




Custom Search