Enums group related values together. Iterating enum entries helps you use all these values one by one.
You want to show all options in a menu made from enum values.
You need to check or process every enum value in a list.
You want to print all enum names for debugging or logging.
Syntax
Kotlin
enumclass EnumName {
ENTRY1,
ENTRY2,
ENTRY3
}
for (entry in EnumName.values()) {
// use entry
}
EnumName.values() returns an array of all enum entries.
You can use a for loop to go through each entry.
Examples
This prints all colors in the Color enum.
Kotlin
enumclass Color {
RED, GREEN, BLUE
}
for (color in Color.values()) {
println(color)
}
Prints each direction with a label.
Kotlin
enumclass Direction {
NORTH, EAST, SOUTH, WEST
}
for (direction in Direction.values()) {
println("Direction: $direction")
}
If an enum has no entries, the loop runs zero times.
Kotlin
enumclass EmptyEnum {
// This enum has no entries
}
for (entry in EmptyEnum.values()) {
println(entry)
}
Sample Program
This program prints all days of the week by iterating over the Day enum entries.
Kotlin
enumclass Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
fun main() {
println("Days of the week:")
for (day in Day.values()) {
println(day)
}
}
OutputSuccess
Important Notes
Time complexity is O(n) where n is the number of enum entries.
Space complexity is O(n) because values() creates an array of all entries.
Common mistake: Trying to modify enum entries during iteration, which is not allowed.
Use iteration when you need to process all enum values. Use direct access when you only need one specific entry.
Summary
Enums group fixed related values.
Use EnumName.values() to get all entries.
Loop over entries to use or display all enum values.
Practice
(1/5)
1. What does EnumName.values() return in Kotlin?
easy
A. An array of all enum entries in the order they are declared
B. A list of enum names as strings
C. The first enum entry only
D. A map of enum entries to their ordinal values
Solution
Step 1: Understand the purpose of values()
The values() function returns all enum entries as an array in the order they are declared.
Step 2: Compare options with this behavior
Only An array of all enum entries in the order they are declared correctly describes this behavior; others describe different data types or partial data.
Final Answer:
An array of all enum entries in the order they are declared -> Option A
Quick Check:
EnumName.values() returns all entries [OK]
Hint: Remember: values() gives all enum entries as an array [OK]
Common Mistakes:
Thinking values() returns strings instead of enum objects
Assuming values() returns only one entry
Confusing values() with valueOf()
2. Which of the following is the correct syntax to iterate over all entries of an enum Color in Kotlin?
easy
A. for (c in Color.values()) { println(c) }
B. for (c in Color) { println(c) }
C. for (c in Color.entries()) { println(c) }
D. for (c in Color.all()) { println(c) }
Solution
Step 1: Recall the correct method to get enum entries
In Kotlin, values() returns all enum entries as an array.
Step 2: Check each option's syntax
The correct syntax is for (c in Color.values()) { println(c) }, which uses Color.values() correctly in a for loop. The other options use invalid syntax: iterating directly over Color, or non-existent methods like entries() or all().
Final Answer:
for (c in Color.values()) { println(c) } -> Option A
Quick Check:
Use values() to loop enums [OK]
Hint: Use EnumName.values() to loop enums [OK]
Common Mistakes:
Trying to loop directly over enum class without values()
Using non-existent methods like entries() or all()
Missing parentheses after values()
3. Given the enum:
enum class Direction { NORTH, EAST, SOUTH, WEST }
What is the output of this code?
for (d in Direction.values()) {
print(d.name.first())
}
medium
A. Direction.NORTHDirection.EASTDirection.SOUTHDirection.WEST
B. north east south west
C. NESW
D. N E S W
Solution
Step 1: Understand the loop and print statement
The loop goes through all enum entries: NORTH, EAST, SOUTH, WEST. For each, it prints the first character of the name.
Step 2: Determine printed characters
First letters are 'N', 'E', 'S', 'W'. They are printed without spaces or newlines.
Final Answer:
NESW -> Option C
Quick Check:
First letters combined = NESW [OK]
Hint: Check what exactly is printed inside the loop [OK]
Common Mistakes:
Assuming spaces or newlines are printed
Printing full names instead of first letters
Confusing enum name with enum value
4. What is wrong with this Kotlin code snippet?
enum class Status { STARTED, RUNNING, FINISHED }
for (s in Status) {
println(s)
}
medium
A. The loop variable must be declared as val
B. Enums cannot be iterated in Kotlin
C. The enum class must be declared inside a function
D. Missing call to values() to get enum entries
Solution
Step 1: Identify how to iterate enums in Kotlin
Enums cannot be iterated directly; you must use Status.values() to get all entries.
Step 2: Check the code for missing values()
The code tries to loop over Status directly, which is invalid syntax.
Final Answer:
Missing call to values() to get enum entries -> Option D
Quick Check:
Use Enum.values() to iterate enums [OK]
Hint: Always use values() to loop enums [OK]
Common Mistakes:
Trying to loop enum class directly without values()
Thinking enums are iterable by default
Adding unnecessary val in loop declaration
5. You have this enum:
enum class Level(val difficulty: Int) {
EASY(1), MEDIUM(2), HARD(3)
}
How do you create a map from each enum entry to its difficulty using iteration?
hard
A. val map = Level.values().map { it.difficulty }
B. val map = Level.values().associateWith { it.difficulty }
C. val map = Level.associate { it to it.difficulty }
D. val map = Level.values().associateBy { it.difficulty }
Solution
Step 1: Understand the goal
We want a map where keys are enum entries and values are their difficulty.
Step 2: Use values() and associateWith
Level.values() gives all entries; associateWith maps each entry to a value.
Step 3: Check options
val map = Level.values().associateWith { it.difficulty } correctly uses associateWith { it.difficulty }. val map = Level.associate { it to it.difficulty } is invalid because Level is a class, not a collection. val map = Level.values().map { it.difficulty } returns a list, not a map. val map = Level.values().associateBy { it.difficulty } maps difficulty to enum, reversing keys and values.
Final Answer:
val map = Level.values().associateWith { it.difficulty } -> Option B
Quick Check:
Use values() + associateWith for map [OK]
Hint: Use values().associateWith { } to map enums [OK]
Common Mistakes:
Trying to call associate on enum class directly
Using map() instead of associateWith() for key-value pairs
Confusing associateBy() which reverses keys and values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.