Kotlin List toLongArray()
Syntax & Examples
Syntax of List.toLongArray()
The syntax of List.toLongArray() extension function is:
fun Collection<Long>.toLongArray(): LongArray
This toLongArray() extension function of List returns an array of Long containing all of the elements of this collection.
✐ Examples
1 Example
In this example,
- We create a list named
longList
containing long values100L, 200L, 300L
. - We use the
toLongArray()
function onlongList
to convert it to a LongArray. - We then print the
longArray
to standard output usingcontentToString()
.
Kotlin Program
fun main(args: Array<String>) {
val longList = listOf(100L, 200L, 300L)
val longArray = longList.toLongArray()
println(longArray.contentToString())
}
Output
[100, 200, 300]
2 Example
In this example,
- We create a list named
longList
containing long values1000L, 2000L, 3000L
. - We use the
toLongArray()
function onlongList
to convert it to a LongArray. - We then print the
longArray
to standard output usingcontentToString()
.
Kotlin Program
fun main(args: Array<String>) {
val longList = listOf(1000L, 2000L, 3000L)
val longArray = longList.toLongArray()
println(longArray.contentToString())
}
Output
[1000, 2000, 3000]
3 Example
In this example,
- We create a list named
longList
containing long values5000L, 6000L, 7000L
. - We use the
toLongArray()
function onlongList
to convert it to a LongArray. - We then print the
longArray
to standard output usingcontentToString()
.
Kotlin Program
fun main(args: Array<String>) {
val longList = listOf(5000L, 6000L, 7000L)
val longArray = longList.toLongArray()
println(longArray.contentToString())
}
Output
[5000, 6000, 7000]
Summary
In this Kotlin tutorial, we learned about toLongArray() extension function of List: the syntax and few working examples with output and detailed explanation for each example.