如何使switch case在Java中接受多种数据类型?
在使用Java的开关大小写时,它只包含char和int,但我想提供字符串大小写。如何做到这一点?如何使switch case在Java中接受多种数据类型?,java,language-features,switch-statement,Java,Language Features,Switch Statement,在使用Java的开关大小写时,它只包含char和int,但我想提供字符串大小写。如何做到这一点?开关盒是一种设计为只接受整数值的语言结构。您不能更改它的工作方式。您还不能在开关盒中使用字符串 它接受的当前类型包括char,byte,short,int,Character,byte,short,Integer,或枚举类型 根据Java语言规范: 表达式的类型必须是char、byte、short、int、Character、byte、short、Integer或枚举类型(§8.9),否则会发生编译时
开关盒是一种设计为只接受整数值的语言结构。您不能更改它的工作方式。您还不能在开关盒中使用字符串
它接受的当前类型包括char
,byte
,short
,int
,Character
,byte
,short
,Integer
,或枚举类型
根据Java语言规范:
表达式的类型必须是char、byte、short、int、Character、byte、short、Integer或枚举类型(§8.9),否则会发生编译时错误
以下所有条件都必须为真,否则将导致编译时错误:
- 与switch语句关联的每个case常量表达式都必须可分配(§5.2)给switch表达式的类型。
- 没有开关标签为空
- 与switch语句关联的两个case常量表达式不能具有相同的值
- 最多一个默认标签可以与同一switch语句关联李>
正如其他答复所述,Java(目前)不支持打开字符串值。实现相同效果的常用方法有三种:
- 如果。。。否则,如果…
语句。(恶心)
enum
类型,其值的名称与字符串相同,使用EType.valueOf(String)将字符串映射到enum值,然后打开enum值。(如果需要遵守枚举值的Java标识符样式规则,或者字符串包含非标识符字符,则可能会更复杂。)String.equals()
的测试/分支要快,但它是O(N)
。。。与编译为跳转表的交换机的O(1)
- 我想你真的很绝望,你可以打开字符串的摘要。如果你对这类事情感兴趣的话
import java.util.zip.Adler32;
public class StringSwitch {
public static void main(String[] args) {
String arg;
if (args == null || args.length == 0) {
arg = "stackoverflow";
} else {
arg = args[0];
}
Adler32 algorithm = new Adler32();
algorithm.update(arg.getBytes());
int hash = (int) algorithm.getValue();
switch (hash) {
case 647693707:
System.out.println("Programming Q & A");
break;
case 145556093:
System.out.println("Narwhals and bacon");
break;
case 193790704:
System.out.println("How do they work?");
break;
}
}
}
我可以打开什么?
从Java 6开始,您只能切换以下类型:
表达式的类型必须是char
、byte
、short
、int
、Character
、byte
、short
、Integer
或枚举类型,否则会发生编译时错误。[…]与switch语句关联的每个case
常量表达式必须可分配给switch表达式的类型
那我该怎么办?
根据您的用例,您可以使用enum
而不是String
。与字符串
不同,可以打开枚举
。下面是一个例子:
public class EnumSwitchSample {
static enum Color {
BLACK, WHITE;
}
public static void main(String[] args) {
test(Color.valueOf("BLACK"));
// "It's black!"
test(Color.WHITE);
// "It's white!"
}
static void test(Color c) {
switch (c) {
case BLACK:
System.out.println("It's black!");
break;
case WHITE:
System.out.println("It's white!");
break;
}
}
}
public class EnumMemberExample {
static enum Mood {
SCREAM("I'M LOUD AND OBNOXIOUS!!!") {
@Override public String say(String statement) {
return statement.toUpperCase().replaceAll("!", "!!!");
}
},
WHISPER("Ssh... Be vewy vewy quiet...") {
@Override public String say(String statement) {
return statement.toLowerCase().replaceAll("!", "...");
}
};
final String msg;
Mood(String msg) { this.msg = msg; }
String getMessage() { return msg; }
abstract String say(String statement);
}
public static void main(String[] args) {
test(Mood.SCREAM);
// "I'M LOUD AND OBNOXIOUS!!!"
// "HELLO!!! HOW ARE YOU!!!"
test(Mood.WHISPER);
// "Ssh... Be vewy vewy quiet..."
// "hello... how are you..."
}
static void test(Mood m) {
System.out.println(m.getMessage());
System.out.println(m.say("Hello! How are you!"));
}
}
然而,Java中的enum
实际上非常强大,您甚至不需要对其进行切换。下面是一个例子:
public class EnumSwitchSample {
static enum Color {
BLACK, WHITE;
}
public static void main(String[] args) {
test(Color.valueOf("BLACK"));
// "It's black!"
test(Color.WHITE);
// "It's white!"
}
static void test(Color c) {
switch (c) {
case BLACK:
System.out.println("It's black!");
break;
case WHITE:
System.out.println("It's white!");
break;
}
}
}
public class EnumMemberExample {
static enum Mood {
SCREAM("I'M LOUD AND OBNOXIOUS!!!") {
@Override public String say(String statement) {
return statement.toUpperCase().replaceAll("!", "!!!");
}
},
WHISPER("Ssh... Be vewy vewy quiet...") {
@Override public String say(String statement) {
return statement.toLowerCase().replaceAll("!", "...");
}
};
final String msg;
Mood(String msg) { this.msg = msg; }
String getMessage() { return msg; }
abstract String say(String statement);
}
public static void main(String[] args) {
test(Mood.SCREAM);
// "I'M LOUD AND OBNOXIOUS!!!"
// "HELLO!!! HOW ARE YOU!!!"
test(Mood.WHISPER);
// "Ssh... Be vewy vewy quiet..."
// "hello... how are you..."
}
static void test(Mood m) {
System.out.println(m.getMessage());
System.out.println(m.say("Hello! How are you!"));
}
}
另见
相关问题
让我戴上我的教条帽子(你是用Java编码的,所以我假设你关心OOP)
而不是问:我如何做错事?相反,要问:什么是正确的做法?在这种情况下:
还有一些讨论:真的很不幸。在其他语言中工作。@标记-其他一些语言。我可以列出一些没有的语言@Mark使用javascript、actionscript等,而不使用C、Java——可以说编译与脚本的区别是安全的吗?同样适用于C。也许区别在于ECMA。你忘了打开字符串哈希代码的经典技巧。switch语句不是编译成哈希映射或类似的东西吗?我个人认为if/elseif链在任何方面都是“恶心的”。@pxl-不这样认为。如果我没记错的话,开关的电平很低。。。基本上相当于一个去。是的,刚刚检查过。。。这是一个很好的机会。我敢肯定我见过人们用开关做疯狂的事情(比如疯狂炫耀)。@CurtainDog-JIT编译器会根据(整数)标签值的密度生成一个分支表或一系列测试和分支。