Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 7 / 68

Working with Lists

In this lesson you will learn the basics of lists in Kotlin so you can handle several pieces of data together. This is for anyone searching for "Kotlin list how to use".

An array (list) is like a shelf of numbered drawers. You create one with listOf() and take items out with their number using withIndex().

The sample code builds a list with val fruits = listOf("apple", "orange", "grape") and uses fruits.withIndex() to take the index and the element together while printing them with numbers. A list created with listOf() is read-only and cannot be changed.

A common stumbling block for beginners is needing mutableListOf() rather than listOf() when the list will change. Adding or removing elements requires a mutable list.

In real-world development, Kotlin's clear separation between read-only and mutable makes it much easier to prevent unintended changes to data.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Kotlin directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)