ObjectSpace Homepage

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

Array examples

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

Array2 - Counting, finding, replacing, removing.

Array3 - Insertion.

Array4 - Reserving capacity.

Array5 - Exceptions.

Array6 - Removal.


Array1 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.Array
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

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

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

    System.out.println( "Iterate through the Array" );
    for ( ArrayIterator i = array.begin(); !i.equals( array.end() ); i.advance() )
      System.out.println( i.get() );
    System.out.println();

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

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

    array.popFront();
    System.out.println( "After popFront() = " + array );

    array.popBack();
    System.out.println( "After popBack() = " + array );
    }
  }

Array1 Example Output

Array( ape, bat, cat )

Enumerate the Array
ape
bat
cat

Iterate through the Array
ape
bat
cat

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

Demonstrate modification
Array( ape, fox, cat )
After popFront() = Array( fox, cat )
After popBack() = Array( fox )

Array2 Example Code

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

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

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

    System.out.println( "array.count( bat ) = " + array.count( "bat" ) );
    int index = array.indexOf( "bat" );
    System.out.println( "array.indexOf( bat ) = " + index );
    array.remove( index );
    System.out.println( "After array.remove( " + index + " ) = " + array );
    array.replace( 0, 2, "bat", "BAT" );
    System.out.println( "After array.replace( 0, 2, bat, BAT ) = " + array );
    System.out.println( "array.remove( cat ) = " + array.remove( "cat" ) );
    System.out.println( "After array.remove( cat ) = " + array );
    array.remove( array.begin() );
    System.out.println( "After array.remove( begin() ) = " + array );
    }
  }

Array2 Example Output

Array( ape, bat, cat, bat, bat, cat )

array.count( bat ) = 3
array.indexOf( bat ) = 1
After array.remove( 1 ) = Array( ape, cat, bat, bat, cat )
After array.replace( 0, 2, bat, BAT ) = Array( ape, cat, BAT, bat, cat )
array.remove( cat ) = 2
After array.remove( cat ) = Array( ape, BAT, bat )
After array.remove( begin() ) = Array( BAT, bat )

Array3 Example Code

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

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

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

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

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

    array.insert( 3, 2, "fox" );
    System.out.println( "After array.insert( 3, 2, fox ) = " + array );
    }
  }

Array3 Example Output

array = Array( bat, cat, dog )

After insert at begin = Array( ape, bat, cat, dog )
After insert at end = Array( ape, bat, cat, dog, emu )
After array.insert( 3, 2, fox ) = Array( ape, bat, cat, fox, fox, dog, emu )

Array4 Example Code

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

/**
 * Reserving capacity.
 *
 * @see com.objectspace.jgl.Array
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Array4
  {
  public static void main( String[] args )
    {
    Object ints[] = { "bat", "cat", "dog" };
    Array array = new Array( ints );
    array.put( 1, "CAT" );
    System.out.println( "array = " + array + ", capacity = " + array.capacity() );
    System.out.print( "array = " );
    for ( int i = 0; i < ints.length; i++ )
      System.out.print( ints[ i ] + " " );
    System.out.println();

    array.ensureCapacity( 100 );
    array.put( 2, "DOG" );
    System.out.println( "array = " + array + ", capacity = " + array.capacity() );
    System.out.print( "array = " );
    for ( int i = 0; i < ints.length; i++ )
      System.out.print( ints[ i ] + " " );
    System.out.println();
    }
  }

Array4 Example Output

array = Array( bat, CAT, dog ), capacity = 3
array = bat cat dog
array = Array( bat, CAT, DOG ), capacity = 100
array = bat cat dog

Array5 Example Code

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

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

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

    }
  }

Array5 Example Output

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

Array6 Example Code

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

/**
 * Removal.
 *
 * @see com.objectspace.jgl.Array
 * @version 3.0.0
 * @author ObjectSpace, Inc.
 */

public class Array6
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    array.add( "Texas" );
    array.add( "Alabama" );
    array.add( "Texas" );
    array.add( "Mississippi" );
    array.add( "Texas" );
    array.add( "Louisiana" );
    array.add( "Texas" );
    array.add( "Alabama" );
    array.add( "Texas" );
    System.out.println( array );

    System.out.println( "removed " + array.remove( "Texas", 4 ) );
    System.out.println( array );

    System.out.println( "removed " + array.remove( "Alabama", 4 ) );
    System.out.println( array );

    System.out.println( "removed " + array.remove( "Georgia", 4 ) );
    System.out.println( array );
    }
  }

Array6 Example Output

Array( Texas, Alabama, Texas, Mississippi, Texas, Louisiana, Texas, Alabama, Texas )
removed 4
Array( Alabama, Mississippi, Louisiana, Alabama, Texas )
removed 2
Array( Mississippi, Louisiana, Texas )
removed 0
Array( Mississippi, Louisiana, Texas )

All Packages  Class Hierarchy  This Package  Previous  Next  Index