使用“静态最终字节”切换大小写
我需要将 switch case
与 byte
内容一起使用。我有 static Final
常量声明如下:
private static final byte[] X_CONST = {2};
private static final byte[] Y_CONST = {3};
然后我想使用如下 switch case :
byte[] x={3};
switch (x[0]){
case X_CONST[0]: ...; break;
case Y_CONST[0]: ...; break;
}
I need to use switch case
with byte
contants. I have static final
constants declared like follows:
private static final byte[] X_CONST = {2};
private static final byte[] Y_CONST = {3};
Then I want to use switch case like follows :
byte[] x={3};
switch (x[0]){
case X_CONST[0]: ...; break;
case Y_CONST[0]: ...; break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该数组可能是静态最终的,但数组的内容不是。因此不允许将其作为 switch 的 case 值,因为该值本身可以在运行时更改。您需要指定
private static Final byte X_CONST = 2
。The array may be static final, but the content of the array isn't. So it isn't allowed as the case value of a switch as the value itself could be changed at runtime. You will need to specify
private static final byte X_CONST = 2
instead.您已将常量声明为字节数组。
Switch 语句不能与数组类型一起使用。
尝试以下声明:
You have declared the constants as byte arrays.
Switch statements cannot be used with array types.
Try the following declaration:
其他答案已经指出了您代码中的问题。作为解决方法,您可以创建一个枚举来使用 switch 语句,例如:
...
Other answers have pointed out the problem in your code. As a workaround, you can create a enum to use a switch statement like:
...
Switch 语句 不能与数组一起使用。
Switch statement cannot be used with array.