Add samples for iterator-related extensions (KT-20357)
This commit is contained in:
committed by
Ilya Gorbunov
parent
94f77c773c
commit
4d13ea89b2
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package samples.collections
|
||||
|
||||
import samples.Sample
|
||||
import java.util.*
|
||||
|
||||
class Iterators {
|
||||
|
||||
@Sample
|
||||
fun iteratorForEnumeration() {
|
||||
val vector = Vector<String>().apply {
|
||||
add("RED")
|
||||
add("GREEN")
|
||||
add("BLUE")
|
||||
}
|
||||
|
||||
for (e in vector.elements()) {
|
||||
println("The element is $e")
|
||||
}
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun iterator() {
|
||||
val mutableList = mutableListOf(1, 2, 3)
|
||||
val mutableIterator = mutableList.iterator()
|
||||
|
||||
if (mutableIterator.hasNext()) {
|
||||
mutableIterator.next()
|
||||
mutableIterator.remove()
|
||||
}
|
||||
|
||||
for (e in mutableIterator) {
|
||||
println("The element is $e")
|
||||
}
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun withIndexIterator() {
|
||||
val iterator = ('a'..'c').iterator()
|
||||
|
||||
for ((index, value) in iterator.withIndex()) {
|
||||
println("The element at $index is $value")
|
||||
}
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun forEachIterator() {
|
||||
val iterator = (1..3).iterator()
|
||||
if (iterator.hasNext()) {
|
||||
iterator.next()
|
||||
}
|
||||
|
||||
iterator.forEach {
|
||||
println("The element is $it")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user