Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

In our September 10, 2005 newsletter, we provided a three-part tutorial that introduced Java generics and showed how to create your own generic methods. In this four-part tutorial,  we present Java 5.0's List collections, which are now implemented as generic classes. Part 1 overviews the classes of the java.util package that implement the List interface-ArrayList, LinkedList and Vector. Parts 2, 3 and 4 present code examples that demonstrate each of these classes. We use various List capabilities and show how iterators can be used to traverse collections to access (and possibly modify) their elements. We also demonstrate Java 5.0's enhanced for statement, which uses a collection's iterator to traverse the collection. This tutorial is intended for students who are already familiar with Java and for Java developers.

[Note: This series of four tutorials (Part 1, Part 2, Part 3, Part 4) is an excerpt (Section 19.5) of Chapter 19, Collections, from our textbook Java How to Program, 6/e. These tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.911-922. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

19.5.2 LinkedList

Figure 19.4 demonstrates operations on LinkedLists. The program creates two LinkedLists that contain 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. 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
 
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. 19.4  Lists and ListIterators.

Page 1 | 2 | 3 | 4

Other Tutorials in this series:

Part 1: Introduction to Lists
Part 2: ArrayList and Iterator
Part 3: LinkedList (Your are here)
Part 4: Vector

Tutorial Index