01: package uk.co.jezuk.mango.algorithms;
02:
03: /**
04: * Searchs the sequence travesed by the Iterator for the given value.
05: * Returns the index of the value in the collection, or <code>-1</code>
06: * if the value is not found. The iterator will have been advanced to
07: * the next object in the sequence.
08: * The objects in the sequence and <code>value</code> must be comparable using
09: * <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: static public int execute(java.util.Iterator iterator, Object value) {
15: if (iterator == null)
16: return -1;
17:
18: int count = 0;
19: while (iterator.hasNext()) {
20: Object obj = iterator.next();
21: if (value.equals(obj))
22: return count;
23: ++count;
24: } // while ...
25:
26: return -1;
27: } // execute
28:
29: private FindPosition() {
30: }
31: } // Find
|