Finding 1 - Finding a single element, conditional searching, finding consecutive elements.
Finding 2 - Finding objects that match a predicate.
Finding1 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import jgl.*; /** * Finding a single element, conditional searching, finding consecutive elements. *
* @see jgl.Finding * @version 1.1 * @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 of saying: 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 GreaterInteger(), 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 EqualTo() ); System.out.println( "First consecutive sequence is of " + 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 is of emu at index 2 Finding1 Example Output
Finding2 Example Code
// Copyright(c) 1996 ObjectSpace, Inc. import jgl.*; /** * Finding objects that match a predicate. *
* @see jgl.Finding * @version 1.1 * @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 Finding2 Example Output