add

open override fun add(element: E): Boolean(source)

Adds the specified element to the end of this list.

Since Kotlin

1.4

Return

true because the list is always modified as the result of this operation.


open override fun add(index: Int, element: E)(source)

Inserts an element into the list at the specified index.

All elements that had indices index .. index + size - 1 are shifted 1 position right.

If index is equal to size, element will be appended to this list.

Since Kotlin

1.4

Throws

if index is less than zero or greater than size of this list.

Samples

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

fun main() { 
   //sampleStart 
   val list = mutableListOf('a', 'b', 'c')

list.add(1, 'c')
println(list) // [a, c, b, c]

list.add(4, 'a')
println(list) // [a, c, b, c, a]

// list.add(100500, 'a') // will fail with IndexOutOfBoundsException 
   //sampleEnd
}