How to use For Loops in Kotlin | for, forEach, forEachIndexed

Опубликовано: 15 Октябрь 2024
на канале: Ludwig Dickmanns
716
33

For Loops and the forEach function!

In this video, you're going to learn how to iterate over collections - or even more generally iterables - in Kotlin. To iterate over, for example, lists you can use the for-in loop. If you want to iterate over the indices, you can use the .indices property. Further, if you want something like a fori loop from Java, you can just use an IntRange with your for in loop. You can even iterate over the chars of a Sting with a for loop. Everything that implements the Iterable interface can be used with the in operator for for loops. If you want to have a bit more functional style, you can also use the .forEach function to iterate over the elements of a list. Finally, you can use the .forEachIndexed function to iterate over the elements with their index in a functional manner.

00:00 Intro
00:18 Iterating over an Iterable
01:14 Iterating over the Indices
01:42 Kotlin fori loop with an IntRange
02:45 forEach function
03:16 forEachIndexed function

Documentation for For Loops in Kotlin:
https://kotlinlang.org/docs/basic-syn...
https://kotlinlang.org/docs/control-f...

@kotlin.learnings on Instagram:
  / kotlin.learnings  
@java.learnings on Instagram:
  / java.learnings  

Thank you for watching!