Array作为switch语句的选项

时间:2008-11-12 02:52:43

标签: java enums switch-statement

我记得在大学时使用带有'二分搜索'或'二进制切换'的开关。类似的东西,我的谷歌foo今天破了。无论如何,它会像这样下降:你定义了一个可能的选项数组(通常是字符串),一些魔法发生,并且数组中的那些选项成为切换中发生的情况。我确实记得他们必须按字母顺序才能工作。你能帮我记住魔法吗?即使是我应该关注的名字也会很棒。

6 个答案:

答案 0 :(得分:10)

我认为你要找的是Enum

从上面的链接...

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;
        }
    }

    ....
}

答案 1 :(得分:1)

你的意思是gperf?或者你可能指的是一般的理论(哈希)?

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

答案 2 :(得分:0)

通常,我不会以这种方式滥用开关(即使我可以)。试试你可能,你将无法在switch语句中使用数组,因为它只允许在case行中使用常量值。你确定你没有考虑下面的某种模式或枚举吗?

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
}

答案 3 :(得分:0)

使用没有开关的枚举看起来像:

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());
    }
}

答案 4 :(得分:0)

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());

我是java enums的新手,但我认为这也应该有用。

答案 5 :(得分:0)

如果您选择使用枚举,并且您的枚举系列使用规模很大:

<强> MAGIC:

如果您正在使用 eclipse ,只要您需要编写以下内容(考虑到已接受的答案代码),它就会为您做一个“魔术”:

switch (day) {
}

选择switch并按 Ctrl + 1 ,魔术就可以了! (所有enum值将填入case块的switch部分中