---
title: "MutableVector"
description: "A [MutableList]-like structure with a simplified interface that offers faster access than
[ArrayList]."
type: "class"
---

<div class='type'>Class</div>


<a id='references'></a>

<div class='sourceset sourceset-common'>Common</div>


```kotlin
public class MutableVector<T>
@PublishedApi
internal constructor(@PublishedApi @JvmField internal var content: Array<T?>, size: Int) :
    RandomAccess
```


A `MutableList`-like structure with a simplified interface that offers faster access than
`ArrayList`.


## Functions

```kotlin
public fun add(element: T): Boolean
```


Adds `element` to the `MutableVector` and returns `true`.


```kotlin
public fun add(index: Int, element: T)
```


Adds `element` to the `MutableVector` at the given `index`, shifting over any elements that
are in the way.


```kotlin
public fun addAll(index: Int, elements: List<T>): Boolean
```


Adds all `elements` to the `MutableVector` at the given `index`, shifting over any elements
that are in the way.


```kotlin
public fun addAll(index: Int, elements: MutableVector<T>): Boolean
```


Adds all `elements` to the `MutableVector` at the given `index`, shifting over any elements
that are in the way.


```kotlin
public inline fun addAll(elements: List<T>): Boolean
```


Adds all `elements` to the end of the `MutableVector` and returns `true` if the
`MutableVector` was changed.


```kotlin
public inline fun addAll(elements: MutableVector<T>): Boolean
```


Adds all `elements` to the end of the `MutableVector` and returns `true` if the
`MutableVector` was changed.


```kotlin
public fun addAll(@Suppress("ArrayReturn") elements: Array<T>): Boolean
```


Adds all `elements` to the end of the `MutableVector` and returns `true` if the
`MutableVector` was changed.


```kotlin
public fun addAll(index: Int, elements: Collection<T>): Boolean
```


Adds all `elements` to the `MutableVector` at the given `index`, shifting over any elements
that are in the way.


```kotlin
public fun addAll(elements: Collection<T>): Boolean
```


Adds all `elements` to the end of the `MutableVector` and returns `true` if the
`MutableVector` was changed.


```kotlin
public inline fun any(predicate: (T) -> Boolean): Boolean
```


Returns `true` if any of the elements give a `true` return value for `predicate`.


```kotlin
public inline fun reversedAny(predicate: (T) -> Boolean): Boolean
```


Returns `true` if any of the elements give a `true` return value for `predicate` while
iterating in the reverse order.


```kotlin
public fun asMutableList(): MutableList<T>
```


Returns `MutableList` interface access to the `MutableVector`.


```kotlin
public fun clear()
```


Removes all elements in the `MutableVector`.


```kotlin
public operator fun contains(element: T): Boolean
```


Returns `true` if the `MutableVector` contains `element` or `false` otherwise.


```kotlin
public fun containsAll(elements: List<T>): Boolean
```


Returns `true` if the `MutableVector` contains all elements in `elements` or `false` if one
or more are missing.


```kotlin
public fun containsAll(elements: Collection<T>): Boolean
```


Returns `true` if the `MutableVector` contains all elements in `elements` or `false` if one
or more are missing.


```kotlin
public fun containsAll(elements: MutableVector<T>): Boolean
```


Returns `true` if the `MutableVector` contains all elements in `elements` or `false` if one
or more are missing.


```kotlin
public fun contentEquals(other: MutableVector<T>): Boolean
```


Returns `true` if the contents of the `MutableVector` are the same or `false` if there is any
difference. This uses equality comparisons on each element rather than reference equality.


```kotlin
public inline fun ensureCapacity(capacity: Int)
```


Ensures that there is enough space to store `capacity` elements in the `MutableVector`.


```kotlin
public fun first(): T
```


Returns the first element in the `MutableVector` or throws a `NoSuchElementException` if it
`isEmpty`.


```kotlin
public inline fun first(predicate: (T) -> Boolean): T
```


Returns the first element in the `MutableVector` for which `predicate` returns `true` or
throws `NoSuchElementException` if nothing matches.


```kotlin
public inline fun firstOrNull(): T?
```


Returns the first element in the `MutableVector` or `null` if it `isEmpty`.


```kotlin
public inline fun firstOrNull(predicate: (T) -> Boolean): T?
```


Returns the first element in the `MutableVector` for which `predicate` returns `true` or
returns `null` if nothing matches.


```kotlin
public inline fun <R> fold(initial: R, operation: (acc: R, T) -> R): R
```


Accumulates values, starting with `initial`, and applying `operation` to each element in the
`MutableVector` in order.


```kotlin
public inline fun <R> foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R): R
```


Accumulates values, starting with `initial`, and applying `operation` to each element in the
`MutableVector` in order.


```kotlin
public inline fun <R> foldRight(initial: R, operation: (T, acc: R) -> R): R
```


Accumulates values, starting with `initial`, and applying `operation` to each element in the
`MutableVector` in reverse order.


```kotlin
public inline fun <R> foldRightIndexed(initial: R, operation: (index: Int, T, acc: R) -> R): R
```


Accumulates values, starting with `initial`, and applying `operation` to each element in the
`MutableVector` in reverse order.


```kotlin
public inline fun forEach(block: (T) -> Unit)
```


Calls `block` for each element in the `MutableVector`, in order.


```kotlin
public inline fun forEachIndexed(block: (Int, T) -> Unit)
```


Calls `block` for each element in the `MutableVector` along with its index, in order.


```kotlin
public inline fun forEachReversed(block: (T) -> Unit)
```


Calls `block` for each element in the `MutableVector` in reverse order.


```kotlin
public inline fun forEachReversedIndexed(block: (Int, T) -> Unit)
```


Calls `block` for each element in the `MutableVector` along with its index, in reverse order.


```kotlin
public inline operator fun get(index: Int): T
```


Returns the element at the given `index`.


```kotlin
public fun indexOf(element: T): Int
```


Returns the index of `element` in the `MutableVector` or `-1` if `element` is not there.


```kotlin
public inline fun indexOfFirst(predicate: (T) -> Boolean): Int
```


Returns the index if the first element in the `MutableVector` for which `predicate` returns
`true`.


```kotlin
public inline fun indexOfLast(predicate: (T) -> Boolean): Int
```


Returns the index if the last element in the `MutableVector` for which `predicate` returns
`true`.


```kotlin
public inline fun isEmpty(): Boolean
```


Returns `true` if the `MutableVector` has no elements in it or `false` otherwise.


```kotlin
public inline fun isNotEmpty(): Boolean
```


Returns `true` if there are elements in the `MutableVector` or `false` if it is empty.


```kotlin
public fun last(): T
```


Returns the last element in the `MutableVector` or throws a `NoSuchElementException` if it
`isEmpty`.


```kotlin
public inline fun last(predicate: (T) -> Boolean): T
```


Returns the last element in the `MutableVector` for which `predicate` returns `true` or
throws `NoSuchElementException` if nothing matches.


```kotlin
public fun lastIndexOf(element: T): Int
```


Returns the index of the last element in the `MutableVector` that is the same as `element` or
`-1` if no elements match.


```kotlin
public inline fun lastOrNull(): T?
```


Returns the last element in the `MutableVector` or `null` if it `isEmpty`.


```kotlin
public inline fun lastOrNull(predicate: (T) -> Boolean): T?
```


Returns the last element in the `MutableVector` for which `predicate` returns `true` or
returns `null` if nothing matches.


```kotlin
public inline fun <reified R> map(transform: (T) -> R): Array<R>
```


Returns an `Array` of results of transforming each element in the `MutableVector`. The Array
will be the same size as this.


```kotlin
public inline fun <reified R> mapIndexed(transform: (index: Int, T) -> R): Array<R>
```


Returns an `Array` of results of transforming each element in the `MutableVector`. The Array
will be the same size as this.


```kotlin
public inline fun <reified R> mapIndexedNotNull(
        transform: (index: Int, T) -> R?
    ): MutableVector<R>
```


Returns an `MutableVector` of results of transforming each element in the `MutableVector`,
excluding those transformed values that are `null`.


```kotlin
public inline fun <reified R> mapNotNull(transform: (T) -> R?): MutableVector<R>
```


Returns an `MutableVector` of results of transforming each element in the `MutableVector`,
excluding those transformed values that are `null`.


```kotlin
public inline operator fun plusAssign(element: T)
```


`add` `element` to the `MutableVector`.


```kotlin
public inline operator fun minusAssign(element: T)
```


`remove` `element` from the `MutableVector`


```kotlin
public fun remove(element: T): Boolean
```


Removes `element` from the `MutableVector`. If `element` was in the `MutableVector` and was
removed, `true` will be returned, or `false` will be returned if the element was not found.


```kotlin
public fun removeAll(elements: List<T>): Boolean
```


Removes all `elements` from the `MutableVector` and returns `true` if anything was removed.


```kotlin
public fun removeAll(elements: MutableVector<T>): Boolean
```


Removes all `elements` from the `MutableVector` and returns `true` if anything was removed.


```kotlin
public fun removeAll(elements: Collection<T>): Boolean
```


Removes all `elements` from the `MutableVector` and returns `true` if anything was removed.


```kotlin
public fun removeAt(index: Int): T
```


Removes the element at the given `index` and returns it.


```kotlin
public fun removeRange(start: Int, end: Int)
```


Removes items from index `start` (inclusive) to `end` (exclusive).


```kotlin
public inline fun removeIf(predicate: (T) -> Boolean)
```


Removes items that satisfy `predicate`


```kotlin
public fun retainAll(elements: Collection<T>): Boolean
```


Keeps only `elements` in the `MutableVector` and removes all other values.


```kotlin
public operator fun set(index: Int, element: T): T
```


Sets the value at `index` to `element`.


```kotlin
public fun sortWith(comparator: Comparator<T>)
```


Sorts the `MutableVector` using `comparator` to order the items.


```kotlin
public inline fun sumBy(selector: (T) -> Int): Int
```


Returns the sum of all values produced by `selector` for each element in the `MutableVector`.



