0
0
Kotlinprogramming~15 mins

Enum entries iteration in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Enum entries iteration
📖 Scenario: You are creating a simple program to list different types of fruits using an enum in Kotlin. You want to show all the fruit names by going through each enum entry.
🎯 Goal: Build a Kotlin program that defines an enum for fruits and then iterates over all enum entries to print their names.
📋 What You'll Learn
Create an enum class called Fruit with entries APPLE, BANANA, and CHERRY
Create a variable called fruitList that holds all entries of the Fruit enum
Use a for loop with variable fruit to iterate over fruitList
Print each fruit name inside the loop using println(fruit)
💡 Why This Matters
🌍 Real World
Enums are used to represent fixed sets of related constants, like days of the week, colors, or fruit types, making code easier to read and maintain.
💼 Career
Understanding enums and how to iterate over them is important for Kotlin developers working on Android apps, backend services, or any Kotlin-based projects.
Progress0 / 4 steps
1
Create the enum class
Create an enum class called Fruit with entries APPLE, BANANA, and CHERRY.
Kotlin
Need a hint?

Use enum class Fruit { APPLE, BANANA, CHERRY } to define the enum.

2
Get all enum entries
Create a variable called fruitList and assign it the value of Fruit.values() to get all enum entries.
Kotlin
Need a hint?

Use val fruitList = Fruit.values() to get all enum entries as an array.

3
Iterate over enum entries
Use a for loop with variable fruit to iterate over fruitList.
Kotlin
Need a hint?

Use for (fruit in fruitList) to loop through all fruits.

4
Print each enum entry
Inside the for loop, print each fruit using println(fruit).
Kotlin
Need a hint?

Use println(fruit) inside the loop to print each fruit name on its own line.