indexOf0 finds the zero-based position of an object in a sequence, for you when you need an index that can be used consistently with the zero-based at0 operator.
Syntax
sequence->indexOf0(object)indexOf0 has the following signature:
indexOf0(object : T) : IntegerReturn value
indexOf0 returns the position of object in self, using zero-based indexing. The first object has index 0.
| Situation | Result |
|---|---|
| The object is at the first position in the sequence | 0
|
| The object is at the third position in the sequence | 2
|
| The object does not exist in the sequence | -1
|
Example
Given a sequence named cars in which toyota is the first object:
cars->indexOf0(toyota)The expression returns 0.
If ford is not in cars:
cars->indexOf0(ford)The expression returns -1.
Use the result with at0
Use indexOf0 when you intend to use the returned index with at0. Check for -1 before attempting to retrieve an object, because -1 means that no matching object was found.
let index : Integer = cars->indexOf0(toyota) in
if index >= 0 then
cars->at0(index)
else
null
endifThis expression returns the object at the found zero-based index, or null when toyota is not in the sequence.
indexOf0 compared with indexOf
indexOf also returns a zero-based index in MDriven, although its name does not make that convention clear. Prefer indexOf0 together with at0 so that the zero-based indexing is explicit. This avoids mixing it with at, which uses one-based indexing.
