KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > co > jezuk > mango > algorithms > FindPosition


1 package uk.co.jezuk.mango.algorithms;
2
3 /**
4  * Searchs the sequence travesed by the Iterator for the given value.
5  * Returns the index of the value in the collection, or <code>-1</code>
6  * if the value is not found. The iterator will have been advanced to
7  * the next object in the sequence.
8  * The objects in the sequence and <code>value</code> must be comparable using
9  * <code>Object.equals</code>.
10  * @see Find
11  * @version $Id: Find.java 93 2004-05-25 20:34:19Z jez $
12  */

13 public class FindPosition
14 {
15   static public int execute(java.util.Iterator JavaDoc iterator, Object JavaDoc value)
16   {
17     if(iterator == null)
18       return -1;
19
20         int count = 0;
21     while(iterator.hasNext())
22     {
23       Object JavaDoc obj = iterator.next();
24       if(value.equals(obj))
25                 return count;
26             ++count;
27     } // while ...
28

29         return -1;
30   } // execute
31

32   private FindPosition() { }
33 } // Find
34
Popular Tags