The forEach
function in Kotlin is used to perform an action on each element of an array. This function is part of the Kotlin standard library and provides a simple and effective way to iterate over the elements of an array and perform operations on them.
Table of Contents
- Introduction
forEach
Function Syntax- Understanding
forEach
- Examples
- Basic Usage
- Using
forEach
with Custom Types - Using
forEachIndexed
- Real-World Use Case
- Conclusion
Introduction
The forEach
function allows you to iterate over each element of an array and apply a given action to each element. It is a concise and readable way to handle array elements without the need for explicit loops.
forEach Function Syntax
The syntax for the forEach
function is as follows:
inline fun <T> Array<out T>.forEach(action: (T) -> Unit): Unit
Parameters:
action
: A lambda function that takes an element of typeT
and performs an operation on it.
Returns:
- This function does not return a value (Unit).
Understanding forEach
The forEach
function is used to apply a specified action to each element in an array. This is particularly useful for operations that need to be performed on each element, such as printing, modifying, or accumulating values.
Examples
Basic Usage
To demonstrate the basic usage of forEach
, we will create an array of integers and print each element using the forEach
function.
Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
numbers.forEach { println(it) }
}
Output:
1
2
3
4
5
Using forEach
with Custom Types
This example shows how to use forEach
to perform an action on each element in an array of custom objects.
Example
class Person(val name: String, val age: Int) {
override fun toString(): String {
return "Person(name='$name', age=$age)"
}
}
fun main() {
val people = arrayOf(
Person("Ravi", 25),
Person("Anjali", 30),
Person("Priya", 22)
)
people.forEach { println(it) }
}
Output:
Person(name='Ravi', age=25)
Person(name='Anjali', age=30)
Person(name='Priya', age=22)
Using forEachIndexed
This example demonstrates how to use forEachIndexed
to perform an action on each element in an array, while also having access to the index of each element.
Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
numbers.forEachIndexed { index, value ->
println("Element at index $index is $value")
}
}
Output:
Element at index 0 is 1
Element at index 1 is 2
Element at index 2 is 3
Element at index 3 is 4
Element at index 4 is 5
Real-World Use Case
Logging Information
In real-world applications, the forEach
function can be used to log information about each element in an array, such as user activity or transaction records.
Example
data class User(val id: Int, val username: String, val email: String)
fun main() {
val users = arrayOf(
User(1, "user1", "user1@example.com"),
User(2, "user2", "user2@example.com"),
User(3, "user3", "user3@example.com")
)
users.forEach { user ->
println("Logging user info: $user")
}
}
Output:
Logging user info: User(id=1, username=user1, email=user1@example.com)
Logging user info: User(id=2, username=user2, email=user2@example.com)
Logging user info: User(id=3, username=user3, email=user3@example.com)
Conclusion
The forEach
function in Kotlin is a convenient method for iterating over elements in an array and performing actions on each element. It is particularly useful for operations that need to be applied to every element in the array, providing a concise and readable alternative to traditional loops.
By understanding and using this function, you can effectively manage element-wise operations in your Kotlin applications.
Comments
Post a Comment
Leave Comment