Kotlin List toIntArray()
Syntax & Examples
Syntax of List.toIntArray()
The syntax of List.toIntArray() extension function is:
fun Collection<Int>.toIntArray(): IntArrayThis toIntArray() extension function of List returns an array of Int containing all of the elements of this collection.
✐ Examples
1 Example
In this example,
- We create a list named
intListcontaining integer values1, 2, 3. - We use the
toIntArray()function onintListto convert it to an IntArray. - We then print the
intArrayto standard output usingcontentToString().
Kotlin Program
fun main(args: Array<String>) {
val intList = listOf(1, 2, 3)
val intArray = intList.toIntArray()
println(intArray.contentToString())
}Output
[1, 2, 3]
2 Example
In this example,
- We create a list named
intListcontaining integer values10, 20, 30. - We use the
toIntArray()function onintListto convert it to an IntArray. - We then print the
intArrayto standard output usingcontentToString().
Kotlin Program
fun main(args: Array<String>) {
val intList = listOf(10, 20, 30)
val intArray = intList.toIntArray()
println(intArray.contentToString())
}Output
[10, 20, 30]
3 Example
In this example,
- We create a list named
intListcontaining integer values0, -1, 5. - We use the
toIntArray()function onintListto convert it to an IntArray. - We then print the
intArrayto standard output usingcontentToString().
Kotlin Program
fun main(args: Array<String>) {
val intList = listOf(0, -1, 5)
val intArray = intList.toIntArray()
println(intArray.contentToString())
}Output
[0, -1, 5]
Summary
In this Kotlin tutorial, we learned about toIntArray() extension function of List: the syntax and few working examples with output and detailed explanation for each example.