asReversed

fun <T> List<T>.asReversed(): List<T>(source)

Returns a reversed read-only view of the original List. All changes made in the original list will be reflected in the reversed one.

Since Kotlin

1.1

Samples


fun main() { 
   //sampleStart 
   val original = mutableListOf('a', 'b', 'c', 'd', 'e')
val originalReadOnly = original as List<Char>
val reversed = originalReadOnly.asReversed()

println(original) // [a, b, c, d, e]
println(reversed) // [e, d, c, b, a]

// changing the original list affects its reversed view
original.add('f')
println(original) // [a, b, c, d, e, f]
println(reversed) // [f, e, d, c, b, a]

original[original.lastIndex] = 'z'
println(original) // [a, b, c, d, e, z]
println(reversed) // [z, e, d, c, b, a] 
   //sampleEnd
}

Returns a reversed mutable view of the original mutable List. All changes made in the original list will be reflected in the reversed one and vice versa.

Since Kotlin

1.1

Samples


fun main() { 
   //sampleStart 
   val original = mutableListOf(1, 2, 3, 4, 5)
val reversed = original.asReversed()

println(original) // [1, 2, 3, 4, 5]
println(reversed) // [5, 4, 3, 2, 1]

// changing the reversed view affects the original list
reversed.add(0)
println(original) // [0, 1, 2, 3, 4, 5]
println(reversed) // [5, 4, 3, 2, 1, 0]

// changing the original list affects its reversed view
original[2] = -original[2]
println(original) // [0, 1, -2, 3, 4, 5]
println(reversed) // [5, 4, 3, -2, 1, 0] 
   //sampleEnd
}
fun <T> List<T>.asReversed(): List<T>(source)

Returns a reversed read-only view of the original List. All changes made in the original list will be reflected in the reversed one.

Since Kotlin

1.0

Samples


fun main() { 
   //sampleStart 
   val original = mutableListOf('a', 'b', 'c', 'd', 'e')
val originalReadOnly = original as List<Char>
val reversed = originalReadOnly.asReversed()

println(original) // [a, b, c, d, e]
println(reversed) // [e, d, c, b, a]

// changing the original list affects its reversed view
original.add('f')
println(original) // [a, b, c, d, e, f]
println(reversed) // [f, e, d, c, b, a]

original[original.lastIndex] = 'z'
println(original) // [a, b, c, d, e, z]
println(reversed) // [z, e, d, c, b, a] 
   //sampleEnd
}

@JvmName(name = "asReversedMutable")
fun <T> MutableList<T>.asReversed(): MutableList<T>(source)

Returns a reversed mutable view of the original mutable List. All changes made in the original list will be reflected in the reversed one and vice versa.

Since Kotlin

1.0

Samples


fun main() { 
   //sampleStart 
   val original = mutableListOf(1, 2, 3, 4, 5)
val reversed = original.asReversed()

println(original) // [1, 2, 3, 4, 5]
println(reversed) // [5, 4, 3, 2, 1]

// changing the reversed view affects the original list
reversed.add(0)
println(original) // [0, 1, 2, 3, 4, 5]
println(reversed) // [5, 4, 3, 2, 1, 0]

// changing the original list affects its reversed view
original[2] = -original[2]
println(original) // [0, 1, -2, 3, 4, 5]
println(reversed) // [5, 4, 3, -2, 1, 0] 
   //sampleEnd
}