Java Reference
In-Depth Information
33 for ( int i = 1; i < words.size(); i++) {
34 if (!words.get(i).equals(words.get(i - 1))) {
35 result.add(words.get(i));
36 }
37 }
38 }
39 return result;
40 }
41 }
The program produces the following output:
list1 = [all, and, bus, go, on, round, round., the, through, town., wheels]
list2 = [all, bus, go, on, swish,, swish., the, through, town., wipers]
The original input files each have 28 words in them. We have reduced the first file
to 11 unique words and the second to 10 unique words. The program is correctly
ignoring differences in case, but it isn't ignoring differences in punctuation. For exam-
ple, it considers "round" and "round." to be different words (one with a period, one
without). Similarly, it considers "swish," (with a comma) and "swish." (with a
period) to be different words. We will fix that problem in Version 3 of our program.
Version 2: Compute Overlap
The first version of the program produces two sorted ArrayList s containing sets of
unique words. For the second version, we want to compute the overlap between the
two lists of words and report it. This operation is complex enough that it deserves to
be in its own method. So, we'll add the following line of code to the main method
right after the two word lists are constructed:
ArrayList<String> common = getOverlap(list1, list2);
The primary task for the second version of our program is to implement the
getOverlap method. Look closely at the two lists of words produced by the first
version:
list1 = [all, and, bus, go, on, round, round., the, through, town., wheels]
list2 = [all, bus, go, on, swish,, swish., the, through, town., wipers]
People are pretty good at finding matches, so you can probably see exactly which
words overlap. Both lists begin with " all " , so that is part of the overlap. Skipping
past the word " and " in the first list, we find the next match is for the word " bus " .
Then we have two more matches with the words " go " and " on " . Next there are a
 
Search WWH ::




Custom Search