Collections: Lists and MutableLists

 




// File:            Main.kt
// Programmer: Engineer Nolverto Urias Obeso
// Creation Date: 06/21/2023
// Email: nolvertou@gmail.com
// Description: Using Collections (lists and mutable lists)

fun main() {
val testList: List<Int> = listOf(11,12,13,14,15,16,17,18,19,20)
println(testList) // Output: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

println(reverseList(testList)) // Output: [20, 19, 18, 17, 16, 15, 14, 13, 12, 11]
println(reverseListAgain(testList)) // Output: [20, 19, 18, 17, 16, 15, 14, 13, 12, 11]
println(testList.reversed()) // Output: [20, 19, 18, 17, 16, 15, 14, 13, 12, 11]
}

fun reverseList(list: List<Int>): List<Int>{
val result: MutableList<Int> = mutableListOf<Int>()
for (i in list.indices){
result.add(list[list.size-i-1])
}
return result
}

fun reverseListAgain(list: List<Int>): List<Int>{
val result: MutableList<Int> = mutableListOf<Int>()
for(i in list.size - 1 downTo 0){
result.add(list[i])
}
return result
}

Comments

Popular posts from this blog