Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Array to List in Kotlin

Tags:

kotlin

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?

like image 730
Audi Avatar asked Oct 10 '17 08:10

Audi


People also ask

What is mutable list in Kotlin?

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.

What is toTypedArray?

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.

How to convert array to set in Kotlin?

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.

What is the difference between LISTOF and mutablelist of Kotlin?

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.

How to convert an array to a list in Python?

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.

How to use ArrayList in Java?

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.


2 Answers

Kotlin support in the standard library this conversion.

You can use directly

disableNos.toList()

or if you want to make it mutable:

disableNos.toMutableList()
like image 180
crgarridos Avatar answered Oct 11 '22 03:10

crgarridos


This will fix your problem :

val disabledNos = intArrayOf(1, 2, 3, 4)
var integers = Arrays.asList(*disabledNos)

Just add * to this to asList

like image 40
Alok Gupta Avatar answered Oct 11 '22 02:10

Alok Gupta