All Packages  Class Hierarchy  Previous  Next  Index

Iterators Examples

Iterators 1 - No description available.

Iterators 2 - No description available.

Iterators 3 - No description available.

Iterators 4 - No description available.

Iterators 5 - No description available.

Iterators 6 - No description available.

Iterators 7 - No description available.

Iterators 8 - No description available.

Iterators 9 - No description available.


Iterators1 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import COM.objectspace.jgl.*;
import java.util.Enumeration;

public class Iterators1
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    array.add( "magical" );
    array.add( "mystery" );
    array.add( "tour" );
    Enumeration iterator = array.elements();
    while ( iterator.hasMoreElements() )
      System.out.println( iterator.nextElement() );
    }
  }

Iterators1 Example Output

magical
mystery
tour

Iterators2 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import COM.objectspace.jgl.*;

public class Iterators2
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    array.add( "magical" );
    array.add( "mystery" );
    array.add( "tour" );
    System.out.println( "before array = " + array );
    toUppercase( array );
    System.out.println( "after array = " + array );

    DList list = new DList();
    list.add( "magical" );
    list.add( "mystery" );
    list.add( "tour" );
    System.out.println( "before list = " + list );
    toUppercase( list );
    System.out.println( "after list = " + list );
    }

  static void toUppercase( Container container )
    {
    // Obtain iterator positioned at first element.
    ForwardIterator iterator = container.start();

    // Obtain iterator positioned immediately after last element.
    ForwardIterator end = container.finish();

    // Loop through every element.
    while ( !iterator.equals( end ) )
      {
      String current = (String) iterator.get();
      // Replace current element with uppercase equivalent.
      iterator.put( current.toUpperCase() );
      iterator.advance(); // Move forward by one element.
      }
    }
  }

Iterators2 Example Output

before array = Array( magical, mystery, tour )
after array = Array( MAGICAL, MYSTERY, TOUR )
before list = DList( magical, mystery, tour )
after list = DList( MAGICAL, MYSTERY, TOUR )

Iterators3 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import COM.objectspace.jgl.*;

public class Iterators3
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    array.add( "ape" );
    array.add( "giraffe" );
    array.add( "lizard" );
    System.out.println( "array = " + array );

    // Obtain iterator positioned "just-past-the-end".
    ArrayIterator iterator = array.end();

    // Obtain iterator positioned at first element.
    ArrayIterator begin = array.begin();

    while ( !iterator.equals( begin ) )
      {
      iterator.retreat();
      System.out.println( iterator.get() );
      }
    }
  }

Iterators3 Example Output

array = Array( ape, giraffe, lizard )
lizard
giraffe
ape

Iterators4 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import COM.objectspace.jgl.*;

public class Iterators4
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    array.add( new Integer( 4 ) );
    array.add( new Integer( 7 ) );
    array.add( new Integer( 2 ) );
    array.add( new Integer( 7 ) );
    array.add( new Integer( 1 ) );
    array.add( new Integer( 7 ) );
    System.out.println( "array = " + array );

    // Obtain iterator positioned at first element.
    ArrayIterator first = array.begin();

    // Obtain iterator positioned immediately after third element.
    ArrayIterator last = array.begin();
    last.advance( 3 );

    // Sort first three elements.
    Sorting.sort( first, last );
    System.out.println( "array = " + array );

    // Replace 7 with 0 in last three elements.
    Replacing.replace( last, array.end(), new Integer( 7 ), new Integer( 0 ) );
    System.out.println( "array = " + array );
    }
  }

Iterators4 Example Output

array = Array( 4, 7, 2, 7, 1, 7 )
array = Array( 2, 4, 7, 7, 1, 7 )
array = Array( 2, 4, 7, 0, 1, 0 )

Iterators5 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import COM.objectspace.jgl.*;

public class Iterators5
  {
  public static void main( String[] args )
    {
    DList list = new DList();
    list.add( "ape" );
    list.add( "bat" );
    list.add( "cat" );
    list.add( "dog" );
    System.out.println( "list = " + list );
    DListIterator iterator = list.find( "cat" );
    System.out.println( "iterator positioned @ " + iterator.get() );
    list.remove( iterator );
    System.out.println( "list = " + list );
    }
  }

Iterators5 Example Output

list = DList( ape, bat, cat, dog )
iterator positioned @ cat
list = DList( ape, bat, dog )

Iterators6 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import COM.objectspace.jgl.*;

public class Iterators6
  {
  public static void main( String[] args )
    {
    HashMap map = new HashMap( true ); // allow duplicates
    map.add( "cat", "beauty" );
    map.add( "dog", "marble" );
    map.add( "cat", "agatha" );
    map.add( "fox", "paula" );
    System.out.println( "map = " + map );

    Range range = map.equalRange( "cat" );
    HashMapIterator iterator = (HashMapIterator) range.begin;
    HashMapIterator last = (HashMapIterator) range.end;
    while ( !iterator.equals( last ) )
      {
      System.out.print( "pair = " + iterator.get() );
      System.out.print( ", key = " + iterator.key() );
      System.out.println( ", value = " + iterator.value() );
      iterator.advance();
      }
    }
  }

Iterators6 Example Output

map = HashMap( Pair( dog, marble ), Pair( fox, paula ), Pair( cat, beauty ), Pair( cat, agatha ) )
pair = Pair( cat, beauty ), key = cat, value = beauty
pair = Pair( cat, agatha ), key = cat, value = agatha

Iterators7 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import COM.objectspace.jgl.*;

public class Iterators7
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    array.add( "ape" );
    array.add( "bat" );
    array.add( "cat" );
    array.add( "dog" );
    System.out.println( "array = " + array );

    ReverseIterator iterator = new ReverseIterator( array.end() );
    while ( iterator.hasMoreElements() )
      System.out.println( iterator.nextElement() );
    }
  }

Iterators7 Example Output

array = Array( ape, bat, cat, dog )
dog
cat
bat
ape

Iterators8 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import COM.objectspace.jgl.*;

public class Iterators8
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    array.add( "ape" );
    array.add( "giraffe" );
    array.add( "elephant" );
    System.out.println( "array = " + array );

    Deque deque = new Deque();
    InsertIterator iterator = new InsertIterator( deque );
    UnaryFunction function = new LengthString();
    Transforming.transform( array, iterator, function );
    System.out.println( "deque = " + deque );
    }
  }

Iterators8 Example Output

array = Array( ape, giraffe, elephant )
deque = Deque( 3, 7, 8 )

Iterators9 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.
import COM.objectspace.jgl.*;
import java.util.Enumeration;

public class Iterators9
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    array.add( "ape" );
    array.add( "bat" );
    array.add( "cat" );

    OutputStreamIterator output = new OutputStreamIterator( System.out );
    Enumeration input = array.elements();

    while ( input.hasMoreElements() )
      output.put( input.nextElement() );
    System.out.println(); // Flush output.

    // You can use an algorithm to do the same thing.
    Copying.copy( array, output );
    System.out.println(); // Flush output.
    }
  }

Iterators9 Example Output

ape bat cat 
ape bat cat 

All Packages  Class Hierarchy  Previous  Next  Index