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.1 ArrayList and Iterator (Continued)

   1  // Fig. 19.3: CollectionTest.java
   2 // Using the Collection interface.
   3 import java.util.List;
   4 import java.util.ArrayList;
   5 import java.util.Collection;
   6 import java.util.Iterator;
   7
   8 public class CollectionTest
   9 {
  10 private static final String[] colors =
  11 { "MAGENTA", "RED", "WHITE", "BLUE", "CYAN" };
  12 private static final String[] removeColors =
  13 { "RED", "WHITE", "BLUE" };
  14
  15 // create ArrayList, add Colors to it and manipulate it
  16 public CollectionTest()
  17 {
  18 List< String > list = new ArrayList< String >();
  19 List< String > removeList = new ArrayList< String >();
  20
  21 // add elements in colors array to list
  22 for ( String color : colors )
  23 list.add( color );
  24
  25 // add elements in removeColors to removeList
  26 for ( String color : removeColors )
  27 removeList.add( color );
  28
  29 System.out.println( "ArrayList: " );
  30
  31 // output list contents
  32 for ( int count = 0; count < list.size(); count++ )
  33 System.out.printf( "%s ", list.get( count ) );
  34
  35 // remove colors contained in removeList
  36 removeColors( list, removeList );
  37
  38 System.out.println( "\n\nArrayList after calling removeColors: " );
  39
  40 // output list contents
  41 for ( String color : list )
  42 System.out.printf( "%s ", color );
  43 } // end CollectionTest constructor
  44
  45 // remove colors specified in collection2 from collection1
  46 private void removeColors(
  47 Collection< String > collection1, Collection< String > collection2 )
  48 {
  49 // get iterator
  50 Iterator< String > iterator = collection1.iterator();
  51
  52 // loop while collection has items
  53 while ( iterator.hasNext() )
  54
  55 if ( collection2.contains( iterator.next() ) )
  56 iterator.remove(); // remove current Color
  57 } // end method removeColors
  58
  59 public static void main( String args[] )
  60 {
  61 new CollectionTest();
  62 } // end main
  63 } // end class CollectionTest
 
ArrayList:
MAGENTA RED WHITE BLUE CYAN

ArrayList after calling removeColors:
MAGENTA CYAN

 Fig. 19.3  Collection interface demonstrated via an ArrayList object.

Page 1 | 2

Other Tutorials in this series:

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

Tutorial Index