Kotlin List toIntArray()
Syntax & Examples


Syntax of List.toIntArray()

The syntax of List.toIntArray() extension function is:

fun Collection<Int>.toIntArray(): IntArray

This 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 intList containing integer values 1, 2, 3.
  • We use the toIntArray() function on intList to convert it to an IntArray.
  • We then print the intArray to standard output using contentToString().

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 intList containing integer values 10, 20, 30.
  • We use the toIntArray() function on intList to convert it to an IntArray.
  • We then print the intArray to standard output using contentToString().

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 intList containing integer values 0, -1, 5.
  • We use the toIntArray() function on intList to convert it to an IntArray.
  • We then print the intArray to standard output using contentToString().

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.