DoubleIterator
An iterator over a sequence of values of type Double
.
This is a substitute for Iterator<Double>
that provides a specialized version of next(): T
method: nextDouble(): Double
and has a special handling by the compiler to avoid platform-specific boxing conversions as a performance optimization.
In the following example:
class DoubleContainer(private val data: DoubleArray) {
// DoubleIterator instead of Iterator<Double> in the signature
operator fun iterator(): DoubleIterator = object : DoubleIterator() {
private var idx = 0
override fun nextDouble(): Double {
if (!hasNext()) throw NoSuchElementException()
return data[idx++]
}
override fun hasNext(): Boolean = idx < data.size
}
}
for (element in DoubleContainer(doubleArrayOf(1.0, 2.0, 3.0))) {
... handle element ...
}
Content copied to clipboard
No boxing conversion is performed during the for-loop iteration. Note that the iterator itself will still be allocated.
Since Kotlin
1.0Functions
Link copied to clipboard
Link copied to clipboard
Returns the next element in the iteration without boxing conversion.
Since Kotlin 1.0
Link copied to clipboard
Returns an Iterator that wraps each element produced by the original iterator into an IndexedValue containing the index of that element and the element itself.
Since Kotlin 1.0