Java Reference
In-Depth Information
31 System.out.println("\"" + target + "\" is word #"
32 + index + " of " + words.size());
33 } else {
34 System.out.println(target + " is not found");
35 }
36 }
37 }
38 }
Here is a sample execution of the program and its resulting output, using a
Scrabble players' dictionary that contains 172,823 words:
Welcome to Scrabble word challenge!
Word to challenge (Enter to quit)? queazy
"queazy" is word #121788 of 172823
Word to challenge (Enter to quit)? kwyjibo
kwyjibo is not found
Word to challenge (Enter to quit)? building
"building" is word #18823 of 172823
Word to challenge (Enter to quit)? java
"java" is word #79156 of 172823
Word to challenge (Enter to quit)? programs
"programs" is word #118860 of 172823
Word to challenge (Enter to quit)?
Sorting
When you use a computer, you often need to sort data. When you browse your hard
drive, for example, you might sort your files by file name, extension, and date. When
you play music, you might sort your song collection by artist, year, or genre. You
might also want to sort arrays and lists so that they can be searched efficiently with
the binary search algorithm.
The Java class libraries provide sorting methods for arrays and lists. You can sort
an array with the Arrays.sort method:
// demonstrate the Arrays.sort method
String[] strings = {"c", "b", "g", "h", "d", "f", "e", "a"};
Arrays.sort(strings);
System.out.println(Arrays.toString(strings));
The preceding code produces the following output:
[a, b, c, d, e, f, g, h]
The array must be of a type that can be compared—that is, a type that stores either
primitives or objects that implement the Comparable interface discussed in Chapter 10.
 
Search WWH ::




Custom Search