Kotlin arrayOf()
Published Dec 2, 2024
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]
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