All Packages  Class Hierarchy  Previous  Next  Index

Counting Examples

Counting 1 - Sum the values of a range.

Counting 2 - Sum the values of a range using a binary function.

Counting 3 - Calculate and sum the difference between adjacent pairs of values.

Counting 4 - Calculate and sum the difference between adjacent pairs of values.

Counting 5 - Calculate and sum the difference between adjacent pairs of values.


Counting1 Example Code

// Copyright(c) 1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Sum the values of a range.
 *
 * @see COM.objectspace.jgl.Counting
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

public class Counting1
  {
  public static void main( String[] args )
    {
    int intArray[] = { 1, 2, 3, 4, 5 };
    IntIterator begin = IntIterator.begin( intArray );
    IntIterator end = IntIterator.end( intArray );

    Number sum = Counting.accumulate( begin, end, new Integer( 0 ) );

    System.out.println( "Sum = " + sum );
    }
  }

Counting1 Example Output

Sum = 15

Counting2 Example Code

// Copyright(c) 1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Sum the values of a range using a binary function.
 *
 * @see COM.objectspace.jgl.Counting
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

public class Counting2
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    array.add( new Long( 5 ) );
    array.add( new Long( 4 ) );
    array.add( new Long( 3 ) );
    array.add( new Long( 2 ) );
    array.add( new Long( 1 ) );

    Number prod = new Long( 1 );
    prod = Counting.accumulate
      (
      array.start(),
      array.finish(),
      prod,
      new TimesNumber( prod.getClass() )
      );

    System.out.println( "Product = " + prod );
    }
  }

Counting2 Example Output

Product = 120

Counting3 Example Code

// Copyright(c) 1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Calculate and sum the difference between adjacent pairs of values.
 *
 * @see COM.objectspace.jgl.Counting
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

public class Counting3
  {
  public static void main( String[] args )
    {
    // create sample array
    int intArray[] = { 1, 2, 4, 8, 16 };
    IntIterator begin = IntIterator.begin( intArray );
    IntIterator end = IntIterator.end( intArray );
    Printing.println( begin, end );

    // make sure destination hase enough space allocated
    Array array = new Array( 5 );

    Counting.adjacentDifference( begin, end, array.start() );
    Printing.println( array );
    }
  }

Counting3 Example Output

( 1, 2, 4, 8, 16 )
( 1, 1, 2, 4, 8 )

Counting4 Example Code

// Copyright(c) 1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Calculate and sum the difference between adjacent pairs of values.
 *
 * @see COM.objectspace.jgl.Counting
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

public class Counting4
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    for ( int i = 0; i < 10; ++i )
      array.add( new Integer( i * i ) );
    System.out.println( array );

    // note that this writes over the original array
    Counting.adjacentDifference( array, array.start() );
    System.out.println( array );
    }
  }

Counting4 Example Output

Array( 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 )
Array( 0, 1, 3, 5, 7, 9, 11, 13, 15, 17 )

Counting5 Example Code

// Copyright(c) 1997 ObjectSpace, Inc.

import COM.objectspace.jgl.*;

/**
 * Calculate and sum the difference between adjacent pairs of values.
 *
 * @see COM.objectspace.jgl.Counting
 * @version 2.0.2
 * @author ObjectSpace, Inc.
 */

public class Counting5
  {
  public static void main( String[] args )
    {
    Array array = new Array();
    for ( long i = 1; i <= 10; ++i )
      array.add( new Long( i ) );
    System.out.println( array );

    Array result = new Array();
    Counting.adjacentDifference
      (
      array,
      result,
      new TimesNumber( new Long( 0 ).getClass() )
      );
    System.out.println( result );
    }
  }

Counting5 Example Output

Array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )
Array( 1, 2, 6, 12, 20, 30, 42, 56, 72, 90 )

All Packages  Class Hierarchy  Previous  Next  Index