I try to do this with (same as java)
val disabledNos = intArrayOf(1, 2, 3, 4)
var integers = Arrays.asList(disabledNos)
but this doesn't give me a list. Any ideas?
Kotlin mutableListOf()MutableList class is used to create mutable lists in which the elements can be added or removed. The method mutableListOf() returns an instance of MutableList Interface and takes the array of a particular type or mixed (depends on the type of MutableList instance) elements or it can be null also.
toTypedArray(): Array<T> Returns a typed array containing all of the elements of this collection. Allocates an array of runtime type T having its size equal to the size of this collection and populates the array with the elements of this collection.
In this program, you'll learn to convert an array to a set and vice versa in Kotlin. When you run the program, the output will be: In the above program, we've an array named array. To convert array to set, we first convert it to a list using asList () as HashSet accepts list as a constructor.
Since there are no list literals in Kotlin. Instead, it's like listOf (1, 2, 3, 4,) for a normal list, and mutableListOf (1, 2, 3, 4,) for a mutable (editable) list. MutableList is basically an ArrayList in Kotlin.
The standard way to convert an array to a list is with the extension function toList (). It returns an immutable list instance. To get a mutable list, you can use the toMutableList () function.
We use ArrayList to access the index of the specified element, convert an Arraylist into string or another array and many more functionalities. 2) ArrayList (capacity: Int): – Creates an ArrayList of specified size. 3) ArrayList (elements: Collection<E>): – Create an ArrayList filled by collection elements.
Kotlin support in the standard library this conversion.
You can use directly
disableNos.toList()
or if you want to make it mutable:
disableNos.toMutableList()
This will fix your problem :
val disabledNos = intArrayOf(1, 2, 3, 4)
var integers = Arrays.asList(*disabledNos)
Just add * to this to asList
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With