Arrays
An array is a data structure that holds a fixed number of values of the same type or its subtypes. The most common type of array in Kotlin is the object-type array, represented by the Array
class.
When to use arrays
Use arrays in Kotlin when you have specialized low-level requirements that you need to meet. For example, if you have performance requirements beyond what is needed for regular applications, or you need to build custom data structures. If you don't have these sorts of restrictions, use collections instead.
Collections have the following benefits compared to arrays:
Collections can be read-only, which gives you more control and allows you to write robust code that has a clear intent.
It is easy to add or remove elements from collections. In comparison, arrays are fixed in size. The only way to add or remove elements from an array is to create a new array each time, which is very inefficient:
fun main() { //sampleStart var riversArray = arrayOf("Nile", "Amazon", "Yangtze") // Using the += assignment operation creates a new riversArray, // copies over the original elements and adds "Mississippi" riversArray += "Mississippi" println(riversArray.joinToString()) // Nile, Amazon, Yangtze, Mississippi //sampleEnd }You can use the equality operator (
==
) to check if collections are structurally equal. You can't use this operator for arrays. Instead, you have to use a special function, which you can read more about in Compare arrays.
For more information about collections, see Collections overview.
Create arrays
To create arrays in Kotlin, you can use:
functions, such as
arrayOf()
,arrayOfNulls()
oremptyArray()
.the
Array
constructor.
This example uses the arrayOf()
function and passes item values to it:
This example uses the arrayOfNulls()
function to create an array of a given size filled with null
elements:
This example uses the emptyArray()
function to create an empty array :
The Array
constructor takes the array size and a function that returns values for array elements given its index:
Nested arrays
Arrays can be nested within each other to create multidimensional arrays:
Access and modify elements
Arrays are always mutable. To access and modify elements in an array, use the indexed access operator[]
:
Arrays in Kotlin are invariant. This means that Kotlin doesn't allow you to assign an Array<String>
to an Array<Any>
to prevent a possible runtime failure. Instead, you can use Array<out Any>
. For more information, see Type Projections.
Work with arrays
In Kotlin, you can work with arrays by using them to pass a variable number of arguments to a function or perform operations on the arrays themselves. For example, comparing arrays, transforming their contents or converting them to collections.
Pass variable number of arguments to a function
In Kotlin, you can pass a variable number of arguments to a function via the vararg
parameter. This is useful when you don't know the number of arguments in advance, like when formatting a message or creating an SQL query.
To pass an array containing a variable number of arguments to a function, use the spread operator (*
). The spread operator passes each element of the array as individual arguments to your chosen function:
For more information, see Variable number of arguments (varargs).
Compare arrays
To compare whether two arrays have the same elements in the same order, use the .contentEquals()
and .contentDeepEquals()
functions:
Transform arrays
Kotlin has many useful functions to transform arrays. This document highlights a few but this isn't an exhaustive list. For the full list of functions, see our API reference.
Sum
To return the sum of all elements in an array, use the .sum()
function:
Shuffle
To randomly shuffle the elements in an array, use the .shuffle()
function:
Convert arrays to collections
If you work with different APIs where some use arrays and some use collections, then you can convert your arrays to collections and vice versa.
Convert to List or Set
To convert an array to a List
or Set
, use the .toList()
and .toSet()
functions.
Convert to Map
To convert an array to a Map
, use the .toMap()
function.
Only an array of Pair<K,V>
can be converted to a Map
. The first value of a Pair
instance becomes a key, and the second becomes a value. This example uses the infix notation to call the to
function to create tuples of Pair
:
Primitive-type arrays
If you use the Array
class with primitive values, these values are boxed into objects. As an alternative, you can use primitive-type arrays, which allow you to store primitives in an array without the side effect of boxing overhead:
Primitive-type array | Equivalent in Java |
---|---|
| |
| |
| |
| |
| |
| |
| |
|
These classes have no inheritance relation to the Array
class, but they have the same set of functions and properties.
This example creates an instance of the IntArray
class:
What's next?
To learn more about why we recommend using collections for most use cases, read our Collections overview.
Learn about other basic types.
If you are a Java developer, read our Java to Kotlin migration guide for Collections.