Left Index

Kotlin mutableListOf()

Mutable(can be modify) lists created with mutableListOf() method where we alter or modify the elements of the list.

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var listItem = mutableListOf("John", "Hary")
	listItem.add("Tony")
	
	// iterate using for loop
	for(index in listItem.indices) {
		println("Item at(${index}): ${listItem.get(index)}")
	}
	
	println()
	
	// iterate using forEachIndexed() method
	listItem.forEachIndexed({i, j -> println("Item at(${i}): ${j}")})
}
Output:
Item at(0): John
Item at(1): Hary
Item at(2): Tony

Item at(0): John
Item at(1): Hary
Item at(2): Tony