Left Index

Kotlin listOf()

Kotlin has two types of lists, immutable lists (i.e. can not be modified) and mutable lists (i.e. can be modified).
Read-only lists are created with listOf() whose elements can not be modified.

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var listItem = listOf("John", "Hary")
	
	// 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(0): John
Item at(1): Hary

Mutable 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

Methods of List
  • sorted(): sorting list item into assending order
  • sortedDescending(): sorting list item into descending order
  • contains(): check for item into list, if exists return true
  • containsAll(): check for list exists into another list
  • count(): total size of list
  • indexOf(): index of given item into list(first occurrence from start)
  • lastIndexOf(): index of given item into list(first occurrence from last)
  • first(): first element of list
  • last(): last element of list
  • forEach(): iterate list item using for loop
  • forEachIndexed(): loop over the list having index and value available in each iteration.

there are many utility methods in list.

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var listItem = mutableListOf("John", "Hary")
	listItem.add("Tony")

	var sortedList = listItem.sorted();

	// iterate using for loop
	for (index in sortedList.indices) {
		println("Item at(${index}): ${sortedList.get(index)}")
	}

	println()
	var sortDescending = listItem.sortedDescending()
	// iterate using for loop
	for (index in sortDescending.indices) {
		println("Item at(${index}): ${sortDescending.get(index)}")
	}

	println()
	var contain = listItem.contains("Hary");
	println("List contain hary: ${contain}")

	var containAll = listItem.containsAll(listOf("Hary", "Tony"))
	println("List contains all: ${containAll}")

	var containAll1 = listItem.containsAll(listOf("Hary", "Bony"))
	println("List contains all: ${containAll1}")

	println("Total size: ${listItem.count()}")

	println("Index of 'Hary' is: ${listItem.indexOf("Hary")}")

	println("Last index of 'Hary' is: ${listItem.lastIndexOf("Hary")}")

	println("First element is: ${listItem.first()}")

	println("Last element is: ${listItem.last()}")

	println()
	listItem.forEach { e -> println("${e}") }
	
	println()
	listItem.forEachIndexed({i, j -> println("value at (${i}) is ${j}")})
}	
Output:
Item at(0): Hary
Item at(1): John
Item at(2): Tony

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

List contain hary: true
List contains all: true
List contains all: false
Total size: 3
Index of 'Hary' is: 1
Last index of 'Hary' is: 1
First element is: John
Last element is: Tony

John
Hary
Tony

value at (0) is John
value at (1) is Hary
value at (2) is Tony