How to initialize a List<T> in Kotlin? Breaking down all the ways
Hi everyone! I just started learning Kotlin after Java, and I ran into a problem: how to properly create and initialize a list? In Java, I used to do it like this:
```java
List list = new ArrayList<>();
list.add("a");
list.add("b");
```
But in Kotlin, I see there are some `listOf(...)`, `mutableListOf(...)`, and you can also use constructors. I tried writing:
```kotlin
val list: List = listOf("a", "b") // this works
list.add("c") // compilation error!
```
Why can't I add an element? And how do I make a mutable list? I also heard about `ArrayList()`, but I don't understand how to use it correctly in Kotlin. Please tell me all the possible ways to initialize lists so I don't get confused anymore. I need both for immutable (read-on
No answers yet.
Log in to leave an answer