I remember from way back at university using a switch with 'binary search' or 'binary switch'. Something like that, My google foo is broken today. Anyway it goes down like this: You define an array of possible options (Strings usually), some magic happens, and those options in the array become the cases in the switch happens. I do remember that they had to be in alphabetical order for this to work. Can you help me remember the magic? Even a name of what I should be looking at would be fantastic.
+8
A:
I think what you are looking for is an Enum.
From the link above...
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
public class EnumTest {
Day day;
public EnumTest(Day day) {
this.day = day;
}
public void tellItLikeItIs() {
switch (day) {
case MONDAY:
System.out.println("Mondays are bad.");
break;
case FRIDAY:
System.out.println("Fridays are better.");
break;
case SATURDAY:
case SUNDAY:
System.out.println("Weekends are best.");
break;
default:
System.out.println("Midweek days are so-so.");
break;
}
}
....
}
tvanfosson
2008-11-12 02:58:44
A:
Normally, I wouldn't abuse a switch in such a way (even if I could). Try you might, you won't be able to get arrays to work in a switch statement because it only allows constant values in the case lines. Are you sure that you are not thinking of some pattern like below or an enumeration?
final int RED = 0;
final int YELLOW = 1;
final int BLUE = 2;
final int GREEN = 3;
String[] colors = new String[] { "red", "yellow", "blue", "green" };
switch (color) {
case RED:
System.out.println(colors[RED]);
break;
case YELLOW:
System.out.println(colors[YELLOW]);
break;
...the rest
}
Elijah
2008-11-12 03:11:26
+1
A:
Did you mean gperf? Or possibly you were referring to the theory (hashing) in general?
HUAGHAGUAH
2008-11-12 03:58:18
A:
Using an Enum without a switch looks like:
public enum Day {
SUNDAY {
public String tellItLikeItIs() {
return "Weekends are best.";
}
},
MONDAY {
public String tellItLikeItIs() {
return "Mondays are bad.";
}
},
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY {
public String tellItLikeItIs() {
return "TGI Friday.";
}
},
SATURDAY {
public String tellItLikeItIs() {
return "Weekends are best.";
}
}
public String tellItLikeItIs() {
return "Midweek days are so-so.";
}
}
public class TodayIs{
public static void main(String... args) {
Day day = Day.valueOf(args[0].toUppercase());
System.out.println(day.tellItLikeItIs());
}
}
Peter Lawrey
2009-02-26 19:53:53
A:
public enum Day {
SUNDAY ("sundays are this"),
MONDAY ("mondays are that"),
TUESDAY ("blah"),
WEDNESDAY ("blah"),
THURSDAY ("blah"),
FRIDAY ("blah"),
SATURDAY ("more blah");
private final String tell;
public Day(String tell){
this.tell = tell;
}
public String tellItLikeItIs() {
return this.tell;
}
}
public class TodayIs{
public static void main(String... args) {
Day day = Day.valueOf(args[0].toUppercase());
System.out.println(day.tellItLikeItIs());
I'm new to java enums, but I think this should work too.
XenF
2009-02-26 20:22:52
Yes, that works too. Only it really has nothing to do with the question anymore (neither does Peter Lawrey's answer).
Michael Myers
2009-02-26 20:40:00