在switch中使用数组作为case语句 [英] Use an array as a case statement in switch

查看:2747
本文介绍了在switch中使用数组作为case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做这样的事情,即在switch语句中使用数组。在Java中有可能吗?如果不是,请解释一个可能的解决方案。

I am trying to do something like this, i.e., use an array in a switch statement. Is it possible in Java? If it isn't, please explain a possible solution.

boolean[] values = new boolean[4];

values[0] = true;
values[1] = false;
values[2] = false;
values[3] = true;

switch (values) {
    case [true, false, true, false]:
        break;
    case [false, false, true, false]:
        break;
    default:
        break;
}


推荐答案

,只是你不能。

SwitchStatement:
    switch ( Expression ) SwitchBlock




Expression的类型必须是char,byte,short,int,Character,Byte,Short,Integer ,String或枚举类型(第8.9节),或发生编译时错误。

The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type (§8.9), or a compile-time error occurs.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.11

这篇关于在switch中使用数组作为case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆