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 typeT
to be included in the array. This is indicated by thevararg
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 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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Kotlin
Learn Kotlin, the expressive, open-source programming language developed by JetBrains.Beginner Friendly9 hours