diff --git a/libraries/stdlib/src/kotlin/JLangIterablesSpecial.kt b/libraries/stdlib/src/kotlin/JLangIterablesSpecial.kt index 3b42803dd31..6917d97b472 100644 --- a/libraries/stdlib/src/kotlin/JLangIterablesSpecial.kt +++ b/libraries/stdlib/src/kotlin/JLangIterablesSpecial.kt @@ -6,6 +6,7 @@ import java.util.List import java.util.AbstractList import java.util.Iterator import java.util.Comparator +import java.util.ArrayList /** * Count the number of elements in collection. @@ -94,16 +95,18 @@ public fun java.lang.Iterable.contains(item : T) : Boolean { } /** - * Convert collection of arbitrary elements to collection of tuples of the index and the element + * Convert collection of arbitrary elements to a List of tuples of the index and the element * * @includeFunctionBody ../../test/ListTest.kt withIndices */ -public fun java.lang.Iterable.withIndices() : java.lang.Iterable<#(Int, T)> { - return object : java.lang.Iterable<#(Int, T)> { - public override fun iterator(): java.util.Iterator<#(Int, T)> { - return NumberedIterator(this@withIndices.iterator()!!) - } +public fun java.lang.Iterable.withIndices(): java.util.List<#(Int, T)> { + val answer = ArrayList<#(Int, T)>() + var nextIndex = 0 + for (e in this) { + answer.add(#(nextIndex, e)) + nextIndex++ } + return answer } public inline fun > java.lang.Iterable.sort() : List { @@ -117,26 +120,3 @@ public inline fun java.lang.Iterable.sort(comparator: java.util.Compar java.util.Collections.sort(list, comparator) return list } - -private class NumberedIterator(private val sourceIterator : java.util.Iterator) : java.util.Iterator<#(Int, TT)> { - private var nextIndex = 0 - - public override fun remove() { - try { - sourceIterator.remove() - nextIndex--; - } catch (e : UnsupportedOperationException) { - throw e - } - } - - public override fun hasNext(): Boolean { - return sourceIterator.hasNext(); - } - - public override fun next(): #(Int, TT) { - val result = #(nextIndex, sourceIterator.next()) - nextIndex++ - return result - } -} \ No newline at end of file