ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

Swap

Swap a value in a MutableList

Source

fun <T> MutableList<T>.swap(index1: Int, index2: Int) {
    val tmp = this[index1] // 'this' corresponds to the list
    this[index1] = this[index2]
    this[index2] = tmp
}

Example

val l = mutableListOf(1, 2, 3)
l.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'l'

Author: Fons

Submitted on: 23 feb 2017

Language: Kotlin

Type: MutableList<T>

Views: 3346