How to initialize an empty MutableList in Kotlin?
Hello! I just started learning Kotlin after Java and ran into a problem when working with lists. I need to create a mutable list (MutableList), but I want it to be initially empty so that I can add elements to it dynamically.
In Java I used to do this:
```java
List list = new ArrayList<>();
```
But in Kotlin I see several ways and don't understand which one is correct. I tried:
```kotlin
val myList = mutableListOf()
```
But the compiler complains: "Type inference failed. Expected type mismatch."
Then I tried:
```kotlin
var myList: MutableList = mutableListOf()
```
This seems to work, but I'm not sure if this is the best way.
I also saw something in the documentation about `ArrayList()` and `emptyList()`, but `emptyList()` returns an immutable
No answers yet.
Log in to leave an answer