Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Iterating Over Collections | Working with Ranges and Collections
Kotlin Loops

bookIterating Over Collections

Desliza para mostrar el menú

Collections in Kotlin are essential tools that let you group and manage multiple values together. The most commonly used collection is the list, which allows you to store an ordered sequence of items. Collections are important in Kotlin because they help you organize data efficiently and enable you to perform operations, such as searching, sorting, or transforming data, in a clean and readable way. Lists, sets, and maps are the primary collection types in Kotlin, with lists being particularly useful when you need to maintain the order of elements or access them by their position.

Main.kt

Main.kt

copy

In this example, the for loop iterates through each element in the numbers list. On each iteration, the loop variable number takes the value of the current item in the list. The statement sum += number adds the current number to the running total stored in sum. After the loop has visited every element, sum contains the total of all values in the list, which is then printed to the console. This approach makes it easy to process all items in a collection without needing to manage indices or worry about the size of the list.

question mark

Which Kotlin collection type allows you to store an ordered list of items?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Sección 2. Capítulo 3
some-alt