java 在switch case java中使用数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27585597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using arrays in switch case java
提问by Kai Arakawa
I have code where what a switch statement is testing for depends on an array variable:
我有代码,其中 switch 语句测试的内容取决于数组变量:
String shuff = Import.shuffle();
String[] form = new String[95];
for(int i = 0; i < 95; i++)
{
form[i] = Format.shuffle(shuff, i);
}
switch(str)
{
case "a":
x = 6;
break;
case "b":
x = 16;
break;
case "c":
x = 23;
break;
//So on and so forth
}
What I want to do is take the array form[] and use it as the case:
我想要做的是采用数组形式[]并使用它作为案例:
String shuff = Import.shuffle();
String[] form = new String[95];
for(int i = 0; i < 95; i++)
{
form[i] = Format.shuffle(shuff, i);
}
switch(str)
{
case form[0]:
x = 6;
break;
case form[1]:
x = 16;
break;
case form[2]:
x = 23;
break;
//So on and so forth
}
But when I try this, it gives the error "case expressions must be constant expressions". Two options to solve this come to mind, but I don't know how to do either. 1. use the array in the switch case somehow 2. use some sort of method that would look like this...
但是当我尝试这个时,它给出了错误“case 表达式必须是常量表达式”。我想到了解决这个问题的两种选择,但我也不知道该怎么做。1. 以某种方式在 switch case 中使用数组 2. 使用某种看起来像这样的方法......
String shuff = Import.shuffle();
String[] form = new String[95];
for(int i = 0; i < 95; i++)
{
form[i] = Format.shuffle(shuff, i);
}
switch(str)
{
case form[0].toString():
x = 6;
break;
case form[1].toString():
x = 16;
break;
case form[2].toString():
x = 23;
break;
//So on and so forth
}
Is there any way to do either?
有什么办法吗?
The Import.shuffle method takes a text file with 95 lines (each line being one character) and strings it together and Format.shuffle puts each of the original lines into individual array variables.
Import.shuffle 方法获取一个包含 95 行(每行一个字符)的文本文件并将其串在一起,Format.shuffle 将每一行原始行放入单独的数组变量中。
I can't convert this into an if else if chain because it's a 95 case switch (edit)
我无法将其转换为 if else if 链,因为它是一个 95 case 开关(编辑)
回答by FlyingPiMonster
You could find the index of str
within form
, then switch based on the index. For example:
你可以找到的索引str
范围内form
,然后切换基于索引。例如:
String shuff = Import.shuffle();
String[] form = new String[95];
for(int i = 0; i < 95; i++)
{
form[i] = Format.shuffle(shuff, i);
}
int index=Arrays.asList(form).indexOf(str);
switch(index)
{
case 0:
x = 6;
break;
case 1:
x = 16;
break;
case 2:
x = 23;
break;
//So on and so forth
}
However, neither of your proposed solutions will work because, when the code is compiled, the compiler needs to know exactly what each case
value is instead of just knowing that it is in a variable. I'm not sure the exact reasons, but it may be for optimization or for making sure cases are not duplicated (which will compile incorrectly, IIRC). The bottom line is that form[0]
is not a constant and the compiler wants a constant.
但是,您提出的两种解决方案都不起作用,因为在编译代码时,编译器需要确切知道每个case
值是什么,而不仅仅是知道它在变量中。我不确定确切的原因,但可能是为了优化或确保案例不重复(这将错误编译,IIRC)。底线是这form[0]
不是一个常量,编译器需要一个常量。
回答by Alexis King
You can't. Java's switch
statement requiresthat case
labels be constant expressions. If your code does not work with that restriction, you'll need to use if...elseif...else
constructions instead.
你不能。Java的switch
声明要求该case
标签是常量表达式。如果您的代码不符合该限制,则您需要改用if...elseif...else
结构。
See §14.11 of the JLS:
请参阅JLS 的 §14.11:
SwitchLabel
:
case
ConstantExpression
:
case
EnumConstantName
:
default :
SwitchLabel
:
case
ConstantExpression
:
case
EnumConstantName
:
default :
回答by Nick Louloudakis
As mentioned before, Java
requires constant expressions in cases on its switch
statements.
如前所述,Java
在其switch
语句的case 中需要常量表达式。
You might need to design the structure better. I noticed that you are making a variable assignment on x, on each case. If this is your real code, you could possible map x
possible values in a Map
, or you could even maped them based on the index, in an int array in order to be possible to retrieve it even faster (with O(1)
complexity).
您可能需要更好地设计结构。我注意到您在每种情况下都对 x 进行了变量赋值。如果这是您的真实代码,您可以x
在 a 中映射可能的值Map
,或者您甚至可以根据索引将它们映射到 int 数组中,以便可以更快地检索它(具有O(1)
复杂性)。
Examples:
例子:
//With maps - based on string values
Map<String, Integer> values = new HashMap<String, Integer>();
values.put(form[0], 6);
values.put(form[1], 16);
values.put(form[2], 23);
//....
x = (int) values.GetValue(str); //str value is your indexer here.
//With arrays, based on indexes
int[] values = new int[] {6, 16, 23/*,...*/};
x = values[i]; // i is your indexer here.
回答by AlanObject
If you really need to do this you will have to use a cascade of if/else. It would be no less efficient than what you imagine the switch statement to be. So:
如果你真的需要这样做,你将不得不使用 if/else 的级联。它的效率不会低于您想象的 switch 语句。所以:
if (str.equals(form[0])) { x = 6; }
else if (str.equals(form[1])) { x = 16; }
else if (str.equals(form[2])) { x = 23; }
Takes many less lines of source as well.
也需要更少的源代码。