Skip to content

Kotlin basics : Using list in the code

Devrath edited this page Dec 24, 2023 · 1 revision
  • List have certain extra functions that are available for manipulating the items.
  • When we have a scenario where there are addition or removel of elements from a collection for prefer the list.
 private fun initiate() {
        // List of cities
        var cityList = listOf("Bangalore","Delhi","Chennai","Calcutta")
        // Not possible since its just a list and not a mutable list
        cityList.add()
        // we can convert one list to another list
        var mutableCityList = cityList.toMutableList()
        // Since now its a mutable list, we can modify the list
        mutableCityList.add("Texas")
        // We can directly add a element at a position also
        mutableCityList.add(1,"Colombo")
        // We can remove a element at a position from th elist 
        mutableCityList.removeAt(2)
}
Clone this wiki locally