- 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:
- 從零開始構建企業級RAG系統
- Python編程自學手冊
- Testing with JUnit
- 編寫整潔的Python代碼(第2版)
- Banana Pi Cookbook
- 自然語言處理Python進階
- PHP+MySQL+Dreamweaver動態網站開發從入門到精通(第3版)
- 輕松上手2D游戲開發:Unity入門
- 微課學人工智能Python編程
- Android應用開發實戰
- Java EE 7 with GlassFish 4 Application Server
- Training Systems Using Python Statistical Modeling
- React and React Native
- Learning PrimeFaces Extensions Development
- 多接入邊緣計算實戰