In this tutorial, you shall learn how to create an array of Byte objects in Kotlin using arrayOf() function, with examples.
Kotlin – Create Byte Array
To create a Byte Array in Kotlin, use arrayOf() function. arrayOf() function creates an array of specified type and given elements.
Syntax
The syntax to create an Array of type Byte
is
arrayOf<Byte>(value1, value2, ...)
where value1, value2, and so on are the Byte values.
Kotlin can infer the type of Array from the argument values. Therefore, we can create a Byte array with initial Byte values as shown in the following.
arrayOf(value1, value2, ...)
If no values are passed to arrayOf() function, then we must specify the datatype Byte as shown in the following.
arrayOf<Byte>()
Examples
1. Create a Byte array with four byte objects
In the following program, we create a Byte Array with four initial elements.
Main.kt
fun main(args: Array<String>) {
val arr = arrayOf<Byte>(25, 41, 85, 3)
for (x in arr) print("$x ")
}
Output
25 41 85 3
Conclusion
In this Kotlin Tutorial, we learned how to create a Byte Array using arrayOf() function, with examples.