J.5.2. LinkedList

Figure J.3 demonstrates various operations on LinkedLists. The program creates two LinkedLists of Strings. The elements of one List are added to the other. Then all the Strings are converted to uppercase, and a range of elements is deleted.


 1   // Fig. J.3: ListTest.java
 2   // Lists, LinkedLists and ListIterators.
 3   import java.util.List;
 4   import java.util.LinkedList;
 5   import java.util.ListIterator;
 6   
 7   public class ListTest
 8   {
 9      public static void main( String[] args )
10      {
11         // add colors elements to list1
12         String[] colors =
13            { "black", "yellow", "green", "blue", "violet", "silver" };
14         List< String > list1 = new LinkedList< String >();
15
16         for ( String color : colors )
17            list1.add( color );
18
19         // add colors2 elements to list2
20         String[] colors2 =
21            { "gold", "white", "brown", "blue", "gray", "silver" };
22         List< String > list2 = new LinkedList< String >();
23
24         for ( String color : colors2 )
25            list2.add( color );
26
27         list1.addAll( list2 ); // concatenate lists
28         list2 = null ; // release resources
29         printList( list1 ); // print list1 elements
30
31         convertToUppercaseStrings( list1 ); // convert to uppercase string
32         printList( list1 ); // print list1 elements
33
34         System.out.print( " Deleting elements 4 to 6..." );
35         removeItems( list1, 4, 7); // remove items 4-6 from list
36         printList( list1 ); // print list1 elements
37         printReversedList( list1 ); // print list in reverse order
38      } // end main
39
40      // output List contents
41      private static void printList( List< String > list )
42      {
43         System.out.println( " list: " );
44
45         for ( String color : list )
46            System.out.printf( "%s ", color );
47
48         System.out.println();
49      } // end method printList
50
51      // locate String objects and convert to uppercase
52      private static void convertToUppercaseStrings( List< String > list )
53      {
54         ListIterator< String > iterator = list.listIterator();
55
56         while ( iterator.hasNext() )
57         {
58            String color = iterator.next(); // get item
59            iterator.set( color.toUpperCase() ); // convert to upper case
60         } // end while
61      } // end method convertToUppercaseStrings
62
63      // obtain sublist and use clear method to delete sublist items
64      private static void removeItems( List< String > list,
65         int start, int end )
66      {
67         list.subList( start, end ).clear(); // remove items
68      } // end method removeItems
69
70      // print reversed list
71      private static void printReversedList( List< String > list )
72      {
73         ListIterator< String > iterator = list.listIterator( list.size() );
74
75         System.out.println( " Reversed List:" );
76
77         // print list in reverse order
78         while ( iterator.hasPrevious() )
79            System.out.printf( "%s ", iterator.previous() );
80      } // end method printReversedList
81   } // end class ListTest

list:
black yellow green blue violet silver gold white brown blue gray silver

list:
BLACK YELLOW GREEN BLUE VIOLET SILVER GOLD WHITE BROWN BLUE GRAY SILVER

Deleting elements 4 to 6...
list:
BLACK YELLOW GREEN BLUE WHITE BROWN BLUE GRAY SILVER

Reversed List:
SILVER GRAY BLUE BROWN WHITE BLUE GREEN YELLOW BLACK


Fig. J.3 | Lists, LinkedLists and ListIterators.

Lines 14 and 22 create LinkedLists list1 and list2 of type String. LinkedList is a generic class that has one type parameter for which we specify the type argument String in this example. Lines 16–17 and 24–25 call List method add to append elements from arrays colors and colors2 to the end of list1 and list2, respectively.

Line 27 calls List method addAll to append all elements of list2 to the end of list1. Line 28 sets list2 to null, so the LinkedList to which list2 referred can be garbage collected. Line 29 calls method printList (lines 41–49) to output list1’s contents. Line 31 calls method convertToUppercaseStrings (lines 52–61) to convert each String element to uppercase, then line 32 calls printList again to display the modified Strings. Line 35 calls method removeItems (lines 64–68) to remove the elements starting at index 4 up to, but not including, index 7 of the list. Line 37 calls method printReversedList (lines 71–80) to print the list in reverse order.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset