

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Iterate over enum
<p>In this article, we will understand how to iterate over enum objects. Enum is a datatype that represents a small collection of objects.</p><p>Below is a demonstration of the same −</p><p><strong>Input</strong></p><p>Suppose our input is −</p><pre class="result notranslate">Enum objects are defined as : red, blue, green, yellow, orange</pre><p><strong>Output</strong></p><p>The desired output would be −</p><pre class="result notranslate">Printing the Objects: red blue green yellow orange</pre><h2>Algorithm</h2><pre class="result notranslate">Step 1 – START Step 2 - Declare the objects of Enum function namely red, blue, green, yellow, orange Step 3 – Using a for loop, iterate over the objects of the enum function and print each object. Step 4- Stop</pre><h2>Example 1</h2><pre class="demo-code notranslate language-java" data-lang="java">enum Enum { red, blue, green, yellow, orange; } public class Colour { public static void main(String[] args) { System.out.println("The values of Enum function are previously defined ."); System.out.println("Accessing each enum constants"); for(Enum colours : Enum.values()) { System.out.print(colours + " "); } } }</pre><h2>Output</h2><pre class="result notranslate">The values of Enum function are previously defined . Accessing each enum constants red blue green yellow orange</pre><h2>Example 2</h2><p>Here is an example to print the days of the week.</p><pre class="demo-code notranslate language-java" data-lang="java">import java.util.EnumSet; enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } public class IterateEnum{ public static void main(String args[]) { Days my_days[] = Days.values(); System.out.println("Values of the enum are: "); EnumSet.allOf(Days.class).forEach(day -> System.out.println(day)); } }</pre><h2>Output</h2><pre class="result notranslate">Values of the enum are: Sunday Monday Tuesday Wednesday Thursday Friday Saturday</pre>
- Related Questions & Answers
- Java Program to Iterate over an ArrayList
- Java Program to Iterate over a HashMap
- Java Program to Iterate over a Set
- Java Program to Iterate over ArrayList using Lambda Expression
- Program to iterate over a List using Java 8 Lambda
- How to iterate over a Java list?
- Iterating over Enum Values in Java
- Program to Iterate over a Stream with Indices in Java 8
- Python program to iterate over multiple lists simultaneously?
- How to iterate over a list in Java?
- How to iterate the values in an enum in Java?
- Iterate over the elements of HashSet in Java
- How to iterate over a C# dictionary?
- How to iterate over a C# list?
- How to iterate over a C# tuple?
Advertisements