-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumMethodsDemo.java
More file actions
29 lines (22 loc) · 870 Bytes
/
EnumMethodsDemo.java
File metadata and controls
29 lines (22 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
ENUM with values(), ordinal(), and valuesOf() DEMONSTRATION PROGRAM IN JAVA
*/
enum Color{
RED, GREEN, BLUE, BLACK, YELLOW;
}
public class EnumMethodsDemo{
public static void main(String[] args) {
Color c1 = Color.GREEN;
Color arr[] = Color.values(); // values() method is used to get values of all named constants of the
// using conventional for loop
System.out.println("=====================\nUsing conventional FOR LOOP\n=======================");
for (int i = 0; i<arr.length; i++ ) {
System.out.println(arr[i] + " is at index [" +i + "]");
}
//using enhanced for loop (only forward processing)
System.out.println("=====================\nUsing Enhanced FOR LOOP\n===========================");
for(Color c : arr){
System.out.println(c + " is at index [ " + c.ordinal() + "]");
}
}
}