public class ListTest
1: // Fig. 19.4: ListTest.java
2: // Using LinkLists.
3: import java.util.List;
4: import java.util.LinkedList;
5: import java.util.ListIterator;
6:
7: public class ListTest
8: {
9: private static final String colors[] = { "black", "yellow",
10: "green", "blue", "violet", "silver" };
11: private static final String colors2[] = { "gold", "white",
12: "brown", "blue", "gray", "silver" };
13:
14: // set up and manipulate LinkedList objects
15: public ListTest()
16: {
17: List< String > list1 = new LinkedList< String >();
18: List< String > list2 = new LinkedList< String >();
19:
20: // add elements to list link
21: for ( String color : colors )
22: list1.add( color );
23:
24: // add elements to list link2
25: for ( String color : colors2 )
26: list2.add( color );
27:
28: list1.addAll( list2 ); // concatenate lists
29: list2 = null; // release resources
30: printList( list1 ); // print list1 elements
31:
32: convertToUppercaseStrings( list1 ); // convert to upper case string
33: printList( list1 ); // print list1 elements
34:
35: System.out.print( "\nDeleting elements 4 to 6..." );
36: removeItems( list1, 4, 7 ); // remove items 4-7 from list
37: printList( list1 ); // print list1 elements
38: printReversedList( list1 ); // print list in reverse order
39: } // end ListTest constructor
40:
41: // output List contents
42: public void printList( List< String > list )
43: {
44: System.out.println( "\nlist: " );
45:
46: for ( String color : list )
47: System.out.printf( "%s ", color );
48:
49: System.out.println();
50: } // end method printList
51:
52: // locate String objects and convert to uppercase
53: private void convertToUppercaseStrings( List< String > list )
54: {
55: ListIterator< String > iterator = list.listIterator();
56:
57: while ( iterator.hasNext() )
58: {
59: String color = iterator.next(); // get item
60: iterator.set( color.toUpperCase() ); // convert to upper case
61: } // end while
62: } // end method convertToUppercaseStrings
63:
64: // obtain sublist and use clear method to delete sublist items
65: private void removeItems( List< String > list, int start, int end )
66: {
67: list.subList( start, end ).clear(); // remove items
68: } // end method removeItems
69:
70: // print reversed list
71: private void printReversedList( List< String > list )
72: {
73: ListIterator< String > iterator = list.listIterator( list.size() );
74:
75: System.out.println( "\nReversed List:" );
76:
77: // print list in reverse order
78: while ( iterator.hasPrevious() )
79: System.out.printf( "%s ", iterator.previous() );
80: } // end method printReversedList
81:
82: public static void main( String args[] )
83: {
84: new ListTest();
85: } // end main
86: } // end class ListTest
87:
88:
89: /**************************************************************************
90: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
91: * Pearson Education, Inc. All Rights Reserved. *
92: * *
93: * DISCLAIMER: The authors and publisher of this book have used their *
94: * best efforts in preparing the book. These efforts include the *
95: * development, research, and testing of the theories and programs *
96: * to determine their effectiveness. The authors and publisher make *
97: * no warranty of any kind, expressed or implied, with regard to these *
98: * programs or to the documentation contained in these books. The authors *
99: * and publisher shall not be liable in any event for incidental or *
100: * consequential damages in connection with, or arising out of, the *
101: * furnishing, performance, or use of these programs. *
102: *************************************************************************/