Use an array as a case statement in switch(在 switch 中使用数组作为 case 语句)
问题描述
我正在尝试做这样的事情,即在 switch 语句中使用数组.在Java中可能吗?如果不是,请说明一个可能的解决方案.
boolean[] values = new boolean[4];值 [0] = 真;值[1] = 假;值[2] = 假;值[3] = 真;开关(值){案例[真,假,真,假]:休息;案例[假,假,真,假]:休息;默认:休息;}NO,你根本做不到.
SwitchStatement:switch ( 表达式 ) SwitchBlock<块引用>
表达式的类型必须是 char、byte、short、int、Character、Byte、Short、Integer、String 或枚举类型(第 8.9 节),否则会发生编译时错误.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.11
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;
}
NO, simply you cannot.
SwitchStatement:
switch ( Expression ) SwitchBlock
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 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 switch 中使用数组作为 case 语句
基础教程推荐
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
