-2
var l=Array<ArrayList<Int>()>(5){};

I tried the above code but, it gave me a compile time error. What is the correct way to achieve this?.

1

1 Answer 1

4

This is the full syntax to do this:

val l: Array<ArrayList<Int>> = Array<ArrayList<Int>>(5) { ArrayList<Int>() }

You can simplify this in a couple of ways, for example you can leave out the type on the left:

val l = Array<ArrayList<Int>>(5) { ArrayList<Int>() }

And then you can also leave out some more of the types, either of these ways:

val l = Array<ArrayList<Int>>(5) { ArrayList() }
val l = Array(5) { ArrayList<Int>() }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, It helped me.

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.