// // Example of using some List classes. // // Usage: // java TryList s1 s2 ... // where s1, s2, etc., are one or more strings. // // Description: // Builds both a LinkedList and a Vector containing s1, s2, etc.; // uses Iterators to print contents. // // Example of use: // java TryList abcd 1234 xy hello abcd xy // (note behavior with duplicates; contrast with TryMap and TrySet) // import java.util.*; public class TryList { public static void main(String[] args) { List l1 = new LinkedList(); List l2 = new Vector(); for (int i = 0; i < args.length; ++i) { l1.add(args[i]); l2.add(args[i]); } System.out.println("contents of LinkedList:"); Iterator iter; iter = l1.iterator(); while (iter.hasNext()) System.out.println(iter.next()); System.out.println("contents of Vector:"); iter = l2.iterator(); while (iter.hasNext()) System.out.println(iter.next()); // note casting of output of get() method. String s = (String) l1.get(0); System.out.println("first element extracted = " + s); } }