1. Introduction

It’s sometimes necessary to convert a list of integers to an integer array for further processing. Fortunately, there are several ways to achieve this in Kotlin.

In this tutorial, we’ll discuss three different methods for converting an integer list to an integer array, along with their corresponding Kotlin code.

2. Using a for Loop

The first approach is the programmatic approach in which we use a simple for loop to iterate over the entries in the list and add each entry to an array:

@Test
fun `convert integer list to integer array using for loop`() {
    val list = listOf(1, 2, 3, 4, 5)
    val expected = intArrayOf(1, 2, 3, 4, 5)
    val result = IntArray(list.size)
    for (i in list.indices) {
        result[i] = list[i]
    }

    assertArrayEquals(expected, result)
}

3. Using the IntArray Constructor

Alternatively, we can obtain an integer array from a list of integers using the IntArray constructor that accepts two arguments – the array size and a lambda expression that returns the value at each index of the array:

@Test
fun `convert integer list to integer array using the intarray constructor`() {
    val list = listOf(1, 2, 3, 4, 5)
    val expected = intArrayOf(1, 2, 3, 4, 5)
    val result = IntArray(list.size) { i -> list[i] }

    assertArrayEquals(expected, result)
}

Basically, we create an array with the same size as the list. Furthermore, the lambda expression copies each element from the list into the result array. It does this by assigning the element at index i from the list to the ‘i‘th index of the result array.

4. Using the toIntArray() Method

The toIntArray() method is a built-in function in Kotlin that we can use to convert a list of integers to an integer array. The result is an integer array containing the same elements as the original list:

@Test
fun `convert integer list to integer array using intArray() method`() {
    val list = listOf(1, 2, 3, 4, 5)
    val expected = intArrayOf(1, 2, 3, 4, 5)
    val result = list.toIntArray()

    assertArrayEquals(expected, result) 
}

Alternatively, we can use the map() method to convert an integer list to an integer array. The map() method helps us iterate the list and create a new list with the same entries as the original list. Finally, we use the toIntArray() method to convert this new list to an integer array:

@Test
fun `convert integer list to integer array using map() method`() {
    val list = listOf(1, 2, 3, 4, 5)
    val expected = intArrayOf(1, 2, 3, 4, 5)
    val result = list.map { it }.toIntArray()

    assertArrayEquals(expected, result)
}

5. Using the toTypedArray() Method

Interestingly, we also have the toTypedArray() method. Basically, we use this method to convert a list to an array of the type inferred by the compiler:

@Test
fun `convert integer list to integer array using toTypedArray() method`() {
    val list = listOf(1, 2, 3, 4, 5)
    val expected = intArrayOf(1, 2, 3, 4, 5)
    val result = list.toTypedArray().toIntArray()

    assertArrayEquals(expected, result)
}

In this example, we use the toTypedArray() method to convert a list of integers to an array. Then finally we call the toIntArray() method to convert the resulting array of Integer objects to an array of primitive integers.

6. Using the stream() Method

The stream() method permits us to convert a list to a stream of integers:

@Test
fun `convert integer list to integer array using stream() method`() {
    val list = listOf(1, 2, 3, 4, 5)
    val expected = intArrayOf(1, 2, 3, 4, 5)
    val result = list.stream().mapToInt { it }.toArray()

    assertArrayEquals(expected, result)
}

Basically, we call the mapToInt() method on the stream to map each integer to its corresponding value. Lastly, we use the toArray() method to obtain an integer array

7. Conclusion

In this article, we’ve explored various methods for converting a list of integers to an integer array in Kotlin. The toIntArray() method is the simplest and most concise way to achieve this, while the copyOf(), stream(), for loop with the index operator and provide alternative options. Furthermore, we wrote a unit test for each method to ascertain correctness.
As always, the source code of the examples is available over on GitHub.
Comments are closed on this article!