Java Reference
In-Depth Information
Table 10.2
ArrayList
Searching Methods
Method
Description
ArrayList<String>
example
returns
true
if the given
contains(value)
list.contains("hello")
value appears in the list
returns the index of the first
indexOf(value)
list.indexOf("world")
occurrence of the given value
in the list (
1
if not found)
returns the index of the last
lastIndexOf(value)
list.lastIndexOf("hello")
occurrence of the given value
in the list (
1
if not found)
All of the
ArrayList
searching methods call the
equals
method for comparing
values. The method names are fairly standard and appear elsewhere in the Java class
libraries. For example, the
String
class also has methods called
indexOf
and
lastIndexOf
that allow you to search for the position of a character or substring
inside a string.
Before we go further, let's look at a complete
ArrayList
program. Search engines
like Google ignore the
stop words
in users' queries. The idea is that certain words like
“a” and “the” appear so often that they aren't worth indexing. Google won't disclose
the exact list of words it uses, although a few examples are listed on the web site and
people have speculated about what they think is on the list.
Google's full list of stop words is believed to have at least 35 entries, but we'll set-
tle for 15 of the most obvious choices. To explore how removing stop words can
affect a text, our program will read a file called
speech.txt
that contains the first
part of Hamlet's famous speech:
To be or not to be - that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles,
And by opposing end them.
The program constructs a list of stop words and then reads the file word by word,
printing every word that is not a stop word. To avoid issues of case, the stop words
are all in lowercase and the call on
contains
is passed a lowercase version of each
word from the input file. Here is the complete program:
1 // This program constructs a list of stop words and echoes
2 // Hamlet's famous speech with the stop words removed.
3
Search WWH ::
Custom Search