- Kotlin for Enterprise Applications using Java EE
- Raghavendra Rao K
- 183字
- 2021-06-10 18:49:20
Iterating over a list
Collections are used for storing and processing a set of objects. Kotlin provides an elegant syntax for iteration over a collection.
Consider the code for 9_IteratingOverList.kts:
val names = listOf("Mark", "Tina", "Williams")
for(name in names) {
println(name)
}
The output is as follows:
If we are interested in getting the index value, we can do that by running the indices command on the collection.
Consider the code for 9a_IteratingOverListIndex.kts:
val names = listOf("Mark", "Tina", "Williams")
for(index in names.indices) {
println(index)
}
The output is as follows:
As the index is a String, we can write an expression to print it. Consider the code for 9b_IteratingOverListIndex.kts:
val names = listOf("Mark", "Tina", "Joseph")
for(index in names.indices) {
println("$index")
}
The output is as follows:
We can also add names to the expression to get items out of the collection, as in the following example, in which we are printing index and name. Consider the code for 9c_IteratingOverList.kts:
val names = listOf("Mark", "Tina", "Joseph")
for(index in names.indices) {
println("$index: ${names.get(index)}")
}
The output is as follows:
- Linux C/C++服務(wù)器開發(fā)實(shí)踐
- Getting Started with ResearchKit
- 架構(gòu)不再難(全5冊(cè))
- Java編程指南:基礎(chǔ)知識(shí)、類庫應(yīng)用及案例設(shè)計(jì)
- 精通軟件性能測試與LoadRunner實(shí)戰(zhàn)(第2版)
- TypeScript實(shí)戰(zhàn)指南
- Easy Web Development with WaveMaker
- Python機(jī)器學(xué)習(xí)經(jīng)典實(shí)例
- C程序設(shè)計(jì)案例教程
- Mastering ServiceNow(Second Edition)
- 軟件品質(zhì)之完美管理:實(shí)戰(zhàn)經(jīng)典
- PHP編程基礎(chǔ)與實(shí)例教程
- Java Fundamentals
- Building Wireless Sensor Networks Using Arduino
- 詳解MATLAB圖形繪制技術(shù)