arrayOf()

MamtaWardhani's avatar
Published Dec 2, 2024
Contribute to Docs

In Kotlin, arrayOf() is used to create a new array containing the specified elements.

Syntax

arrayOf(vararg elements: T): Array<T>
  • elements: T: A variable number of elements of type T to be included in the array. This is indicated by the vararg keyword, allowing any number of elements to be passed.

Example

This example demonstrates the use of arrayOf() to create an array with a specified set of elements:

fun main() {
val array = arrayOf(10, 20, 30, 40, 50)
// Print the contents of the array
println(array.contentToString())
}

The above code produces the following output:

[10, 20, 30, 40, 50]

All contributors

Contribute to Docs

Learn Kotlin on Codecademy