package alphabetizewords;
import java.util.*;
public class Alphabetize {
public static void main(String[] args) {
//... Declare variables.
Scanner in = new Scanner(System.in);
ArrayListwords = new ArrayList ();
//... Read input one word at a time.
System.out.println("Enter words. End with EOF (CTRL-Z then Enter)");
System.out.println(" or click Close Input in NetBeans.");
//... Read input one word at a time, adding it to an array list.
while (in.hasNext()) {
words.add(in.next());
}
//... Sort the words.
Collections.sort(words);
//... Print the sorted list.
System.out.println("\n\nSorted words\n");
for (String word : words) {
System.out.println(word);
}
}
}
Tuesday, 21 September 2010
Alphabetize.java (Arraylist example)
Labels:
ArrayList,
Collections,
java,
list
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment