Avatar ·

How to add an element to ArrayList in Kotlin: a complete guide for beginners

Hello everyone! I just started learning Kotlin and switched from Java. In Java, I'm used to working with ArrayList and adding elements using the `.add()` method. But in Kotlin, I ran into a problem. I have a simple code: ```kotlin val myList = ArrayList() myList.add("Hello") ``` But Android Studio underlines `myList` and says that the variable should be `var`, not `val`. When I change it to `var`, everything works, but I don't understand why. After all, in Java you can declare `final ArrayList list = new ArrayList<>();` and then safely add elements. Plus, I've seen many people use `mutableListOf()` instead of `ArrayList`. What's the difference? And are there other ways to add an element to a list? For example, how to add multiple elements at once? Or how to add an element at a specific position?