All Packages Class Hierarchy This Package Previous Next Index
Deque1 - Construction, enumeration, access, pushing, popping.
Deque2 - Counting, finding, erasing, replacing, removing.
Deque3 - Insertion.
Deque4 - Exceptions.
Deque1 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,Deque
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Deque1
{
public static void main( String[] args )
{
Deque deque = new Deque();
deque.pushBack( "bat" );
deque.add( "cat" );
deque.pushFront( "ape" );
System.out.println( deque );
System.out.println();
System.out.println( "Enumerate the Deque" );
Enumeration e = deque.elements();
while ( e.hasMoreElements() )
System.out.println( e.nextElement() );
System.out.println();
System.out.println( "Iterate through the Deque" );
for ( DequeIterator i = deque.begin(); !i.equals( deque.end() ); i.advance() )
System.out.println( i.get() );
System.out.println();
System.out.println( "Demonstrate access" );
System.out.println( "deque.at( 0 ) = " + deque.at( 0 ) );
System.out.println( "deque.front() = " + deque.front() );
System.out.println( "deque.at( 2 ) = " + deque.at( 2 ) );
System.out.println( "deque.back() = " + deque.back() );
System.out.println();
System.out.println( "Demonstrate modification" );
deque.put( 1, "fox" );
System.out.println( deque );
deque.popFront();
System.out.println( "After popFront() = " + deque );
deque.popBack();
System.out.println( "After popBack() = " + deque );
}
}
Deque( ape, bat, cat )
Enumerate the Deque
ape
bat
cat
Iterate through the Deque
ape
bat
cat
Demonstrate access
deque.at( 0 ) = ape
deque.front() = ape
deque.at( 2 ) = cat
deque.back() = cat
Demonstrate modification
Deque( ape, fox, cat )
After popFront() = Deque( fox, cat )
After popBack() = Deque( fox )
Deque2 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
/**
* Counting, finding, erasing, replacing, removing.
*
* @see com.objectspace.jgl,Deque
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Deque2
{
public static void main( String[] args )
{
Deque deque = new Deque();
deque.add( "ape" );
deque.add( "bat" );
deque.add( "cat" );
deque.add( "bat" );
deque.add( "bat" );
deque.add( "cat" );
System.out.println( deque );
System.out.println();
System.out.println( "deque.count( bat ) = " + deque.count( "bat" ) );
int index = deque.indexOf( "bat" );
System.out.println( "deque.indexOf( bat ) = " + index );
deque.remove( index );
System.out.println( "After deque.remove( " + index + " ) = " + deque );
deque.replace( 0, 2, "bat", "BAT" );
System.out.println( "After deque.replace( 0, 2, bat, BAT ) = " + deque );
System.out.println( "deque.remove( cat ) = " + deque.remove( "cat" ) );
System.out.println( "After deque.remove( cat ) = " + deque );
deque.remove( deque.begin() );
System.out.println( "After deque.remove( begin() ) = " + deque );
}
}
Deque( ape, bat, cat, bat, bat, cat )
deque.count( bat ) = 3
deque.indexOf( bat ) = 1
After deque.remove( 1 ) = Deque( ape, cat, bat, bat, cat )
After deque.replace( 0, 2, bat, BAT ) = Deque( ape, cat, BAT, bat, cat )
deque.remove( cat ) = 2
After deque.remove( cat ) = Deque( ape, BAT, bat )
After deque.remove( begin() ) = Deque( BAT, bat )
Deque3 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
/**
* Insertion.
*
* @see com.objectspace.jgl,Deque
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Deque3
{
public static void main( String[] args )
{
Deque deque = new Deque();
deque.add( "bat" );
deque.add( "cat" );
deque.add( "dog" );
System.out.println( "deque = " + deque );
System.out.println();
deque.insert( deque.begin(), "ape" );
System.out.println( "After insert at begin = " + deque );
deque.insert( deque.end(), "emu" );
System.out.println( "After insert at end = " + deque );
deque.insert( 3, 2, "fox" );
System.out.println( "After deque.insert( 3, 2, fox ) = " + deque );
}
}
deque = Deque( bat, cat, dog )
After insert at begin = Deque( ape, bat, cat, dog )
After insert at end = Deque( ape, bat, cat, dog, emu )
After deque.insert( 3, 2, fox ) = Deque( ape, bat, cat, fox, fox, dog, emu )
Deque4 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
/**
* Exceptions.
*
* @see com.objectspace.jgl,Deque
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Deque4
{
public static void main( String[] args )
{
Deque deque = new Deque();
try
{
deque.popBack();
}
catch ( InvalidOperationException exception )
{
System.out.println( "Caught " + exception );
}
deque.add( "ape" );
deque.add( "bat" );
deque.add( "cat" );
try
{
deque.at( 5 );
}
catch ( IndexOutOfBoundsException exception )
{
System.out.println( "Caught " + exception );
}
}
}
Caught com.objectspace.jgl.InvalidOperationException: Deque is empty
Caught java.lang.IndexOutOfBoundsException: Attempt to access index 5 when valid range is 0..2
All Packages Class Hierarchy This Package Previous Next Index