enum

Hi,

With method ordinal() in enum we can get the position for that particular constant. But I would like to know, is there any way to get the value when we give the ordinal. Here is the code below.

importstatic java.lang.System.out;

import java.util.*;

enum Day{

MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

publicstatic String getDay(Day day){

return day.toString().toUpperCase();

}

publicstatic String getDay(String day){

return day.toUpperCase();

}

publicstaticint ordinal(String day){

return Day.valueOf(day.toUpperCase()).ordinal() + 1;

}

}

publicclass EnumDemo{

publicstaticvoid main(String ... args){

Day[] d = Day.values();

for(Day day : d) out.println("day = "+day+"\t|\t "+

Day.valueOf(day.toString())+" \t|\t "+

Day.ordinal(day.toString()));

}

}

I wrote those methods getDay(Day), getDay(String), ordinal(String) to print the values when we pass the values from command line arguments. But I cut that as it is not relavent here.

[2172 byte] By [srisria] at [2007-10-3 4:56:14]
# 1

enum Day {

MONDAY(2), TUESDAY(4), WEDNESDAY(6), THURSDAY(8), FRIDAY(10), SATURDAY(12), SUNDAY(14);

private final int ord;

Day(int ord) {

this.ord = ord;

}

public int getOrd() {

return ord;

}

public static Day valueOf(int ord) {

for (Day d : values()) {

if (d.getOrd() == ord)

return d;

}

return null;

}

...

public static int ordinal(String day) {

return Day.valueOf(day.toUpperCase()).getOrd();

}

}

jfbrierea at 2007-7-14 23:01:26 > top of Java-index,Java Essentials,Java Programming...
# 2
Edit: sorry I wrote ****. It's quite early in the morning...
Mongera at 2007-7-14 23:01:26 > top of Java-index,Java Essentials,Java Programming...
# 3
Thank you very much. I just wanted to know is there any direct method to it or we have to do it on our own. And thanks for showing me how to code it also.
srisria at 2007-7-14 23:01:26 > top of Java-index,Java Essentials,Java Programming...