小编Zai*_*tsu的帖子

Java foreach循环不必要的浮动到int转换

Vector<Vector<Integer>> wavesInformation;

for(Vector<Integer> waveInformation : wavesInformation) {

    for(Integer enemyIndex : waveInformation) { 

    }

}
Run Code Online (Sandbox Code Playgroud)

给出运行时错误:

Exception in thread "LWJGL Application" java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Integer
Run Code Online (Sandbox Code Playgroud)

for(Integer enemyIndex : waveInformation) {行.

这很混乱,因为没有使用浮动.

写作

for(Float enemyIndex : waveInformation) {   
Run Code Online (Sandbox Code Playgroud)

给出编译错误:

Type mismatch: cannot convert from element type Integer to Float
Run Code Online (Sandbox Code Playgroud)

编辑:

wavesInformation在调试模式下查找,发现存储的数字实际上是浮点数(0.0).

EDIT2:

嗯,这真的很奇怪.

出于实验目的,我将for循环更改为:

for(int i = 0 ; i < waveInformation.size() ; i++) {
Run Code Online (Sandbox Code Playgroud)

我试图将值waveInformation赋给变量,如下所示:

float x = waveInformation.get(i);
Run Code Online (Sandbox Code Playgroud)

和: …

java foreach json vector libgdx

0
推荐指数
1
解决办法
287
查看次数

标签 统计

foreach ×1

java ×1

json ×1

libgdx ×1

vector ×1