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

bookIterating Over Collections

Deslize para mostrar o menu

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?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 2. Capítulo 3
some-alt