50

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?

1
  • 1
    The main problem why it didn't work in the first place, was, that you basically created a List<IntArray>. You need to use the spread operator (*) to get a List<Int>, i.e. the following would have worked too: val integers = Arrays.asList(*disabledNos). A similar question, that also mentions the actual error: Convert Array<String> to ArrayList<String> Commented Apr 1, 2019 at 9:29

3 Answers 3

104

Kotlin support in the standard library this conversion.

You can use directly

disableNos.toList()

or if you want to make it mutable:

disableNos.toMutableList()
Sign up to request clarification or add additional context in comments.

Comments

2

This will fix your problem :

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

Just add * to this to asList

Comments

0

Oops that was very simple:

var integers = disabledNos.toList()

2 Comments

This will give you List<IntArray> instead of List<Int>. @crgarridos's answer is correct.
Oh boy I forgot to remove Arrays.asList() while posting the answer :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.