ObjectSpace Homepage

JGL - The Generic Collection Library for Java
All Packages  Class Hierarchy  This Package  Previous  Next  Index

DList examples

DList1 - Construction, enumeration, access, pushing, popping.

DList2 - Counting, finding, erasing, replacing, removing.

DList3 - Insertion.

DList4 - Exceptions.

DList5 - Splicing a DList into another.

DList6 - Splicing a piece of a DList into another.

DList7 - Reversing, removing, filtering.

DList8 - Splicing a DList into another.


DList1 Example Code

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

/**
 * Construction, enumeration, access, pushing, popping.
 *
 * @see com.objectspace.jgl.DList
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class DList1
  {
  public static void main( String[] args )
    {
    DList dlist = new DList();
    dlist.pushBack( "bat" );
    dlist.add( "cat" );
    dlist.pushFront( "ape" );
    System.out.println( dlist );
    System.out.println();

    System.out.println( "Enumerate the DList" );
    Enumeration e = dlist.elements();
    while ( e.hasMoreElements() )
      System.out.println( e.nextElement() );
    System.out.println();

    System.out.println( "Iterate through the DList" );
    for ( DListIterator i = dlist.begin(); !i.equals( dlist.end() ); i.advance() )
      System.out.println( i.get() );
    System.out.println();

    System.out.println( "Demonstrate access" );
    System.out.println( "dlist.at( 0 ) = " + dlist.at( 0 ) );
    System.out.println( "dlist.front() = " + dlist.front() );
    System.out.println( "dlist.at( 2 ) = " + dlist.at( 2 ) );
    System.out.println( "dlist.back() = " + dlist.back() );
    System.out.println();

    System.out.println( "Demonstrate modification" );
    dlist.put( 1, "fox" );
    System.out.println( dlist );

    System.out.println( "popFront() returns: " + dlist.popFront() );
    System.out.println( "After popFront() = " + dlist );

    System.out.println( "popBack() returns: " + dlist.popBack() );
    System.out.println( "After popBack() = " + dlist );
    }
  }

DList1 Example Output

DList( ape, bat, cat )

Enumerate the DList
ape
bat
cat

Iterate through the DList
ape
bat
cat

Demonstrate access
dlist.at( 0 ) = ape
dlist.front() = ape
dlist.at( 2 ) = cat
dlist.back() = cat

Demonstrate modification
DList( ape, fox, cat )
popFront() returns: ape
After popFront() = DList( fox, cat )
popBack() returns: cat
After popBack() = DList( fox )

DList2 Example Code

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

/**
 * Counting, finding, erasing, replacing, removing.
 *
 * @see com.objectspace.jgl.DList
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class DList2
  {
  public static void main( String[] args )
    {
    DList dlist = new DList();
    dlist.add( "ape" );
    dlist.add( "bat" );
    dlist.add( "cat" );
    dlist.add( "bat" );
    dlist.add( "bat" );
    dlist.add( "cat" );
    System.out.println( dlist );
    System.out.println();

    System.out.println( "dlist.count( bat ) = " + dlist.count( "bat" ) );
    DListIterator iterator = dlist.find( "bat" );
    if ( !iterator.atEnd() )
      {
      System.out.println( "object at dlist.find( bat ) = " + iterator.get() );
      dlist.remove( iterator );
      System.out.println( "After dlist.remove( iterator ) = " + dlist );
      }

    DListIterator start = dlist.begin();
    DListIterator finish = dlist.begin();
    finish.advance( 3 );
    dlist.replace( start, finish, "bat", "BAT" );
    System.out.println( "After dlist.replace( start, finish, bat, BAT ) = " + dlist );

    System.out.println( "dlist.remove( cat ) = " + dlist.remove( "cat" ) );
    System.out.println( "After dlist.remove( cat ) = " + dlist );
    dlist.remove( dlist.begin() );
    System.out.println( "After dlist.remove( begin() ) = " + dlist );
    }
  }

DList2 Example Output

DList( ape, bat, cat, bat, bat, cat )

dlist.count( bat ) = 3
object at dlist.find( bat ) = bat
After dlist.remove( iterator ) = DList( ape, cat, bat, bat, cat )
After dlist.replace( start, finish, bat, BAT ) = DList( ape, cat, BAT, bat, cat )
dlist.remove( cat ) = 2
After dlist.remove( cat ) = DList( ape, BAT, bat )
After dlist.remove( begin() ) = DList( BAT, bat )

DList3 Example Code

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

/**
 * Insertion.
 *
 * @see com.objectspace.jgl.DList
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class DList3
  {
  public static void main( String[] args )
    {
    DList dlist = new DList();
    dlist.add( "bat" );
    dlist.add( "cat" );
    dlist.add( "dog" );
    System.out.println( "dlist = " + dlist );
    System.out.println();

    dlist.insert( dlist.begin(), "ape" );
    System.out.println( "After insert at begin = " + dlist );

    dlist.insert( dlist.end(), "emu" );
    System.out.println( "After insert at end = " + dlist );

    DListIterator i = dlist.begin();
    i.advance( 3 );
    dlist.insert( i, 2, "fox" );
    System.out.println( "After dlist.insert( i, 2, fox ) = " + dlist );
    }
  }

DList3 Example Output

dlist = DList( bat, cat, dog )

After insert at begin = DList( ape, bat, cat, dog )
After insert at end = DList( ape, bat, cat, dog, emu )
After dlist.insert( i, 2, fox ) = DList( ape, bat, cat, fox, fox, dog, emu )

DList4 Example Code

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

/**
 * Exceptions.
 *
 * @see com.objectspace.jgl.DList
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class DList4
  {
  public static void main( String[] args )
    {
    DList dlist = new DList();
    try
      {
      dlist.popBack();
      }
    catch ( InvalidOperationException exception )
      {
      System.out.println( "Caught " + exception );
      }
    dlist.add( "ape" );
    dlist.add( "bat" );
    dlist.add( "cat" );
    try
      {
      dlist.at( 5 );
      }
    catch ( IndexOutOfBoundsException exception )
      {
      System.out.println( "Caught " + exception );
      }

    }
  }

DList4 Example Output

Caught com.objectspace.jgl.InvalidOperationException: DList is empty
Caught java.lang.IndexOutOfBoundsException: Attempt to access index 5 when valid range is 0..2

DList5 Example Code

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

/**
 * Splicing a DList into another.
 *
 * @see com.objectspace.jgl.DList
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class DList5
  {
  public static void main( String[] args )
    {
    DList dlist1 = new DList();
    dlist1.add( "apple" );
    dlist1.add( "banana" );
    DList dlist2 = new DList();
    dlist2.add( "lotus" );
    dlist2.add( "ferrari" );
    dlist2.add( "lamborghini" );
    System.out.println( "before: dlist1 = " + dlist1 + ", dlist2 = " + dlist2 );

    dlist1.splice( dlist1.begin(), dlist2 );
    System.out.println( "after: dlist1 = " + dlist1 + ", dlist2 = " + dlist2 );
    }
  }

DList5 Example Output

before: dlist1 = DList( apple, banana ), dlist2 = DList( lotus, ferrari, lamborghini )
after: dlist1 = DList( lotus, ferrari, lamborghini, apple, banana ), dlist2 = DList()

DList6 Example Code

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

/**
 * Splicing a piece of a DList into another.
 *
 * @see com.objectspace.jgl.DList
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class DList6
  {
  public static void main( String[] args )
    {
    DList dlist1 = new DList();
    dlist1.add( "apple" );
    dlist1.add( "banana" );
    DList dlist2 = new DList();
    dlist2.add( "lotus" );
    dlist2.add( "ferrari" );
    dlist2.add( "lamborghini" );
    System.out.println( "before: dlist1 = " + dlist1 + ", dlist2 = " + dlist2 );

    DListIterator i = dlist1.begin();
    i.advance();
    DListIterator start = dlist2.begin();
    DListIterator finish = dlist2.begin();
    finish.advance( 2 );
    dlist1.splice( i, dlist2, start, finish );
    System.out.println( "after: dlist1 = " + dlist1 + ", dlist2 = " + dlist2 );
    }
  }

DList6 Example Output

before: dlist1 = DList( apple, banana ), dlist2 = DList( lotus, ferrari, lamborghini )
after: dlist1 = DList( apple, lotus, ferrari, banana ), dlist2 = DList( lamborghini )

DList7 Example Code

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

/**
 * Reversing, removing, filtering.
 *
 * @see com.objectspace.jgl.DList
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class DList7
  {
  public static void main( String[] args )
    {
    DList dlist = new DList();
    dlist.add( "x" );
    dlist.add( "l" );
    dlist.add( "x" );
    dlist.add( "g" );
    dlist.add( "s" );
    dlist.add( "s" );
    System.out.println( "dlist = " + dlist );

    dlist.reverse();
    System.out.println( "After dlist.reverse() = " + dlist );

    dlist.remove( "x" );
    System.out.println( "After dlist.remove( x ) = " + dlist );

    dlist.unique();
    System.out.println( "After dlist.unique() = " + dlist );
    }
  }

DList7 Example Output

dlist = DList( x, l, x, g, s, s )
After dlist.reverse() = DList( s, s, g, x, l, x )
After dlist.remove( x ) = DList( s, s, g, l )
After dlist.unique() = DList( s, g, l )

DList8 Example Code

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

/**
 * Splicing a DList into another.
 *
 * @see com.objectspace.jgl.DList
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class DList8
  {
  public static void main( String[] args )
    {
    DList dlist = new DList();
    dlist.add( "apple" );
    dlist.add( "banana" );
    dlist.add( "lotus" );
    dlist.add( "ferrari" );
    dlist.add( "lamborghini" );
    System.out.println( "dlist = " + dlist );

    try
      {
      dlist.splice( dlist.begin(), dlist, dlist.begin(), dlist.end() );
      }
    catch ( InvalidOperationException ioe )
      {
      System.out.println( "Caught: " + ioe );
      }
    }
  }

DList8 Example Output

dlist = DList( apple, banana, lotus, ferrari, lamborghini )

All Packages  Class Hierarchy  This Package  Previous  Next  Index