All Packages Class Hierarchy This Package Previous Next Index
Finding1 - Finding a single element, conditional searching, finding consecutive elements.
Finding2 - Finding objects that match a predicate.
Finding1 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.adapters.*;
import com.objectspace.jgl.algorithms.*;
import com.objectspace.jgl.predicates.*;
/**
* Finding a single element, conditional searching, finding consecutive elements.
*
* @see com.objectspace.jgl.algorithms.Finding
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Finding1
{
public static void main( String[] args )
{
Array array = new Array();
array.add( "cat" );
array.add( "dog" );
array.add( "emu" );
array.add( "dog" );
array.add( "dog" );
ArrayIterator i = array.begin();
while ( true )
{
i = (ArrayIterator)Finding.find( i, array.end(), "dog" );
if ( i.atEnd() ) // A simpler way: if ( i.equals( array.end() ) )...
break;
System.out.println
(
"iterator found " + i.get() + " at index " + i.index()
);
i.advance();
}
int intArray[] = { 3, 6, 2, 1, 8, 9, 4, 5 };
IntIterator j = (IntIterator)Finding.findIf
(
IntIterator.begin( intArray ),
IntIterator.end( intArray ),
new BindSecondPredicate( new GreaterNumber(), new Integer( 7 ) )
);
System.out.println
(
"First element > 7 is " + j.get() + " at index " + j.index()
);
DList list = new DList();
list.add( "cat" );
list.add( "dog" );
list.add( "emu" );
list.add( "emu" );
list.add( "dog" );
DListIterator k = (DListIterator)Finding.adjacentFind( list, new com.objectspace.jgl.predicates.EqualTo() );
System.out.println
(
"First consecutive sequence: " + k.get() + " at index " + k.index()
);
}
}
iterator found dog at index 1
iterator found dog at index 3
iterator found dog at index 4
First element > 7 is 8 at index 4
First consecutive sequence: emu at index 2
Finding2 Example Code
// Copyright(c) 1996,1997 ObjectSpace, Inc.
import com.objectspace.jgl.*;
import com.objectspace.jgl.algorithms.*;
/**
* Finding objects that match a predicate.
*
* @see com.objectspace.jgl.algorithms.Finding
* @version 3.0.0
* @author ObjectSpace, Inc.
*/
public class Finding2
{
public static void main( String[] args )
{
Array array = new Array();
array.add( "cat" );
array.add( "monkey" );
array.add( "lion" );
array.add( "armadillo" );
array.add( "zebra" );
System.out.println( "array = " + array );
System.out.println
(
"Array has SOME string > 5 chars == "
+ Finding.some( array, new Finding2UnaryPredicate() )
);
System.out.println
(
"Array has EVERY string > 5 chars == "
+ Finding.every( array, new Finding2UnaryPredicate() )
);
System.out.println
(
"1st Object in array > 5 chars == "
+ Finding.detect( array, new Finding2UnaryPredicate() )
);
}
}
class Finding2UnaryPredicate implements UnaryPredicate
{
// return true if the length of the toString() is
// greater than 5.
public boolean execute( Object object )
{
return object.toString().length() > 5;
}
}
array = Array( cat, monkey, lion, armadillo, zebra )
Array has SOME string > 5 chars == true
Array has EVERY string > 5 chars == false
1st Object in array > 5 chars == monkey
All Packages Class Hierarchy This Package Previous Next Index