All Packages  Class Hierarchy  Previous  Next  Index

SList Examples

SList 1 - Construction, enumeration, access, pushing, popping.

SList 2 - Counting, finding, erasing, replacing, removing.

SList 3 - Insertion.

SList 4 - Exceptions.

SList 5 - Splicing a SList into another.

SList 6 - Splicing a piece of a SList into another.

SList 7 - Removing.


SList1 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.

import java.util.Enumeration;
import COM.objectspace.jgl.*;

/**
 * Construction, enumeration, access, pushing, popping.
 *
 * @see COM.objectspace.jgl.SListist
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

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

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

    System.out.println( "Iterate through the SList" );
    for ( SListIterator i = list.begin(); !i.equals( list.end() ); i.advance() )
      System.out.println( i.get() );
    System.out.println();

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

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

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

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

SList1 Example Output

SList( ape, bat, cat )

Enumerate the SList
ape
bat
cat

Iterate through the SList
ape
bat
cat

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

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

SList2 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Counting, finding, erasing, replacing, removing.
 *
 * @see COM.objectspace.jgl.SList
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

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

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

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

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

SList2 Example Output

SList( ape, bat, cat, bat, bat, cat )

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

SList3 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Insertion.
 *
 * @see COM.objectspace.jgl.SList
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

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

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

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

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

SList3 Example Output

list = SList( bat, cat, dog )

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

SList4 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Exceptions.
 *
 * @see COM.objectspace.jgl.SList
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

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

    }
  }

SList4 Example Output

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

SList5 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Splicing a SList into another.
 *
 * @see COM.objectspace.jgl.SList
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

public class SList5
  {
  public static void main( String[] args )
    {
    SList list1 = new SList();
    list1.add( "apple" );
    list1.add( "banana" );
    SList list2 = new SList();
    list2.add( "lotus" );
    list2.add( "ferrari" );
    list2.add( "lamborghini" );
    System.out.println( "before: list1 = " + list1 + ", list2 = " + list2 );

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

SList5 Example Output

before: list1 = SList( apple, banana ), list2 = SList( lotus, ferrari, lamborghini )
after: list1 = SList( lotus, ferrari, lamborghini, apple, banana ), list2 = SList()

SList6 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Splicing a piece of a SList into another.
 *
 * @see COM.objectspace.jgl.Vector
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

public class SList6
  {
  public static void main( String[] args )
    {
    SList list1 = new SList();
    list1.add( "apple" );
    list1.add( "banana" );
    SList list2 = new SList();
    list2.add( "lotus" );
    list2.add( "ferrari" );
    list2.add( "lamborghini" );
    System.out.println( "before: list1 = " + list1 + ", list2 = " + list2 );

    SListIterator i = list1.begin();
    i.advance();
    SListIterator start = list2.begin();
    SListIterator finish = list2.begin();
    finish.advance( 2 );
    list1.splice( i, list2, start, finish );
    System.out.println( "after: list1 = " + list1 + ", list2 = " + list2 );
    }
  }

SList6 Example Output

before: list1 = SList( apple, banana ), list2 = SList( lotus, ferrari, lamborghini )
after: list1 = SList( apple, lotus, ferrari, banana ), list2 = SList( lamborghini )

SList7 Example Code

// Copyright(c) 1996,1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Removing.
 *
 * @see COM.objectspace.jgl.SList
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

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

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

SList7 Example Output

list = SList( x, l, x, g, s, s )
After list.remove( x ) = SList( l, g, s, s )

All Packages  Class Hierarchy  Previous  Next  Index