The beauty of immutable arrays

Immutability is one of the key principles of modern programming languages. So, it's obvious that Kotlin also has immutability implementation. Because of this, Kotlin's collections package has treated immutable data structures as first-class citizens. A few examples might make you understand this more:

val days = listOf("Sunday", "Monday", "Tuesday", "Wednesday")val months = arrayListOf("January", "February", "March", "April")

In the preceding snippet, days is an immutable list whereas months is a mutable one. For example, months.add("May") is a valid statement whereas we cannot add an item to the days list. The only way to do so is to get a new list out of the existing one by adding the new item. Here's the ...

Get Hands-On Data Structures and Algorithms with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.