Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize Kotlin IntArray from IntRange?

Tags:

kotlin

I'm trying to initialize an IntArray in Kotlin like so:

intArrayOf(1..9)

But I get a TypeError that Int is required, but I'm providing an IntRange. Is there a way to initialize the array with a range, or do I have to explicitly write out each value?

like image 239
nclarkclt Avatar asked Mar 17 '19 18:03

nclarkclt


People also ask

How do I initialize IntArray Kotlin?

To initialize primitive arrays with a specific value, you can use the class constructor with lambda. For example, IntArray(5) creates an integer array of size 5 and initializes it with a value of 1 .

How do you use Kotlin IntArray?

IntArray is a class in Kotlin representing the array of elements. Each instance of this class is represented as an integer array. To the constructor of this class you need to pass the number of elements you need in the array (size). By default, all the elements of the created array will be initialized to "0".

How do you initialize a Boolean array in Kotlin?

In Kotlin, arrays can be created using the function arrayOf() or using an Array constructor. Arrays are stored in a sequence as per the memory location is concerned. All the elements in an array can be accessed using their index. Arrays are mutable in nature.

How do you initialize an array with 0 in Kotlin?

In Kotlin There are Several Ways. Then simply initial value from users or from another collection or wherever you want. var arr = Array(size){0} // it will create an integer array var arr = Array<String>(size){"$it"} // this will create array with "0", "1", "2" and so on.

How to initialize an array with values in Kotlin?

All the items stored in a Kotlin array are called elements that can be accessed with the help of their index numbers starting from 0. Using the arrayOf () function and array constructor. Initialize an array with values while declaring it.

How do you sort an array of objects in Kotlin?

An array stores multiple items together for easy sorting and searching through a collection of elements. All the items stored in a Kotlin array are called elements that can be accessed with the help of their index numbers starting from 0. Using the arrayOf () function and array constructor.

How to convert enumerated values to an array in Kotlin?

Kotlin has a built-in arrayOf method that converts the provided enumerated values into an array of the given type: 3. Primitive Arrays We can also use the arrayOf method with primitive values. However, Kotlin will autobox the primitive values to their corresponding object wrapper classes which will have detrimental performance implications.

How to avoid overhead in Kotlin programming?

To avoid this overhead Kotlin has wide support for primitive arrays. There are dedicated arrayOf methods for the following types: double, float, long, int, char, short, byte, boolean. We can easily initialize a primitive int array using its dedicated arrayOf method: 4. Late Initialization With Indices


Video Answer


1 Answers

Using built in functions, this is how you could get to an IntArray from an IntRange:

val array: IntArray = (1..9).toList().toIntArray()

This is a bit wasteful, because it first constructs a list where it puts all the elements, and then it constructs an array as well. To do this directly, you could use your own extension, something like...

fun IntRange.toIntArray(): IntArray {
    if (last < first)
        return IntArray(0)

    val result = IntArray(last - first + 1)
    var index = 0
    for (element in this)
        result[index++] = element
    return result
}

Which would give you this syntax:

val array: IntArray = (1..9).toIntArray()
like image 57
zsmb13 Avatar answered Sep 17 '22 14:09

zsmb13