Wednesday, September 22, 2010

Alphabetize.java (Arraylist example)

package alphabetizewords;

import java.util.*;

public class Alphabetize {

public static void main(String[] args) {
//... Declare variables.
Scanner in = new Scanner(System.in);
ArrayList words = 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);
}
}
}

No comments:

Post a Comment

Chitika