subList

expect open override fun subList(fromIndex: Int, toIndex: Int): MutableList<E>(source)

Returns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive). The returned list is backed by this list, so changes in the returned list are reflected in this list, and vice versa.

Structural changes in the base list make the behavior of the view unspecified.

Since Kotlin

1.0

Throws

if fromIndex less than zero or toIndex greater than size of this list.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3, 4, 5)
println(list.subList(2, 4)) // [3, 4]

val mutableList = mutableListOf(1, 2, 3, 4, 5)
val subList = mutableList.subList(2, 4)
println(subList) // [3, 4]

mutableList[3] = -1
println(subList) // [3, -1]

// list.subList(0, 100) // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual open override fun subList(fromIndex: Int, toIndex: Int): MutableList<E>(source)

Returns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive). The returned list is backed by this list, so changes in the returned list are reflected in this list, and vice-versa.

Structural changes in the base list make the behavior of the view unspecified.

Since Kotlin

1.3

Throws

if fromIndex less than zero or toIndex greater than size of this list.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3, 4, 5)
println(list.subList(2, 4)) // [3, 4]

val mutableList = mutableListOf(1, 2, 3, 4, 5)
val subList = mutableList.subList(2, 4)
println(subList) // [3, 4]

mutableList[3] = -1
println(subList) // [3, -1]

// list.subList(0, 100) // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual open override fun subList(fromIndex: Int, toIndex: Int): MutableList<E>(source)

Returns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive). The returned list is backed by this list, so changes in the returned list are reflected in this list, and vice-versa.

Structural changes in the base list make the behavior of the view unspecified.

Since Kotlin

1.8

Throws

if fromIndex less than zero or toIndex greater than size of this list.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3, 4, 5)
println(list.subList(2, 4)) // [3, 4]

val mutableList = mutableListOf(1, 2, 3, 4, 5)
val subList = mutableList.subList(2, 4)
println(subList) // [3, 4]

mutableList[3] = -1
println(subList) // [3, -1]

// list.subList(0, 100) // will fail with IndexOutOfBoundsException 
   //sampleEnd
}
actual open override fun subList(fromIndex: Int, toIndex: Int): MutableList<E>(source)

Returns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive). The returned list is backed by this list, so changes in the returned list are reflected in this list, and vice-versa.

Structural changes in the base list make the behavior of the view unspecified.

Since Kotlin

1.8

Throws

if fromIndex less than zero or toIndex greater than size of this list.

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val list = listOf(1, 2, 3, 4, 5)
println(list.subList(2, 4)) // [3, 4]

val mutableList = mutableListOf(1, 2, 3, 4, 5)
val subList = mutableList.subList(2, 4)
println(subList) // [3, 4]

mutableList[3] = -1
println(subList) // [3, -1]

// list.subList(0, 100) // will fail with IndexOutOfBoundsException 
   //sampleEnd
}