Kotlin arrayOf()
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 typeTto be included in the array. This is indicated by thevarargkeyword, 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 arrayprintln(array.contentToString())}
The above code produces the following output:
[10, 20, 30, 40, 50]
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Kotlin on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
- Beginner Friendly.9 hours