views:

248

answers:

5

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
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
+1  A: 

Did you mean gperf? Or possibly you were referring to the theory (hashing) in general?

http://www.gnu.org/software/gperf/

HUAGHAGUAH
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
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
Yes, that works too. Only it really has nothing to do with the question anymore (neither does Peter Lawrey's answer).
Michael Myers