// CSE 143, Winter 2010, Marty Stepp // This program demonstrates the ArrayList class. // It reads words from a file into an array list, then prints them, // then filters out all of the plural words and prints again. import java.io.*; import java.util.*; public class ProcessFile { public static void main(String[] args) throws FileNotFoundException { // String[] allWords = new String[1000]; ArrayList allWords = new ArrayList(); Scanner input = new Scanner(new File("words.txt")); while (input.hasNext()) { String word = input.next(); allWords.add(word); } System.out.println(allWords); for (int i = 0; i < allWords.size(); i++) { String word = allWords.get(i); if (word.endsWith("s")) { allWords.remove(i); i--; } } // // alternate version that goes from the back of the list // for (int i = allWords.size() - 1; i >= 0; i--) { // String word = allWords.get(i); // if (word.endsWith("s")) { // allWords.remove(i); // } // } System.out.println(allWords); /* // some other code we wrote to play with the list // print the list in reverse order for (int i = allWords.size() - 1; i >= 0; i--) { System.out.println(allWords.get(i)); } // add and remove some elements allWords.remove(2); System.out.println(allWords); allWords.add(5, "booyah"); System.out.println(allWords); // swap two elements String temp = allWords.get(0); allWords.set(0, allWords.get(7)); allWords.set(7, temp); System.out.println(allWords); */ } }