import java.util.*; import java.io.*; public class Remove { public static void main(String[] args) throws IOException { PrintStream out = System.out; if (args.length != 2) { out.println("Usage: java Remove N"); out.println("where is linked or array"); out.println("and N is the maximum word length to keep"); System.exit(1); } // create the dictionary list String listType = "ArrayList"; List dictionary = new ArrayList(); if (args[0].equals("linked")) { listType = "LinkedList"; dictionary = new LinkedList(); } // maximum word length final int N = Integer.parseInt(args[1]); // read in the dictionary using a scanner final String DICT_NAME = "wordsEn.txt"; Scanner dictionaryInput = new Scanner(new File(DICT_NAME)); while (dictionaryInput.hasNextLine()) { String word = dictionaryInput.nextLine().trim(); dictionary.add(word); } int nRemoved = 0; long startTime = System.nanoTime(); Iterator iter = dictionary.iterator(); while (iter.hasNext()) { String word = iter.next(); if (word.length() > N) { iter.remove(); nRemoved++; } } long estimatedTime = System.nanoTime() - startTime; out.printf("Took %d ns to remove %d words longer than %d using a %s%n%n", estimatedTime, nRemoved, N, listType); } }