Challenge: Arrays & Lists |

This is a companion discussion topic for the original entry at

When I convert the array to list using this method:
val gamesList = mutableListOf(gamesArray)

The following both lines result in a build error

But if the array is made into a list using the following:
val gamesList = gamesArray.toMutableList()

The code compiles without error.

Why the difference when both methods create a MutableList type?

Hey @pallavis!

When you use the first approach:

val gamesList = mutableListOf(gamesArray)

You actually create a MutableList<Array>. Because of that, you can only add elements to the list that are arrays too. It doesn’t flatten them at all.

However, the second approach:

val gamesList = gamesArray.toMutableList()

You take the array and convert it into a mutable list. So you get a MutableList essentially. That way you can add elements from other lists or remove specific games.

Hope this makes sense! :]


val gamesList = mutableListOf(gamesArray) creates a two-dimensional collection (though in my case it has one element only - the gamesArray).

Whereas val gamesList = gamesArray.toMutableList() creates a list out of the elements of gamesArray.

Thank you, Filip. :+1:t3: