This article explores different ways to generate a list of consecutive integers in Kotlin.

1. Using List constructor

The List constructor creates a new list with the specified size, where each element is calculated by calling the specified function sequentially starting from the beginning. Here’s how the code would look like:

Download Code

2. Using Kotlin Ranges

Another alternative uses the Kotlin Range to generate a range of increasing integers between the specified indices, and then collect all elements into a list.

Download Code

 
Alternatively, you can generate the Kotlin range of the list’s size and map each value to the next number in a sequence.

Download Code

 
To fill an “existing” list with the increasing integer sequence, do as follows:

Download Code

 
Or, start from index 0:

Download Code

That’s all about generating a list of consecutive integers in Kotlin.