use*_*020 0 java arrays for-loop list
for循环在下面java代码停止在第一次迭代中,我不知道为什么!你能帮助我吗?
controllerSwitches元素是{1,2}
allSwithces元素是{1,2,3,4}
for (int p = 0; p < controllerSwitches.length; p++)
{
switches tempSwitch = controllerSwitches[p];
for (int w = 0; w <= allSwithces.size(); w++)
{
System.out.println(tempSwitch.getSwitchId() +"\t" + allSwithces.get(w).getSwitchId());
if (allSwithces.get(w).getSwitchId().equals(tempSwitch.getSwitchId()))
{
failedControllerswitches.add(allSwithces.get(w)); // it is break after getting the first p index
}
continue;
}
continue;
}
Run Code Online (Sandbox Code Playgroud)
它获得第一个p索引并将其与allSwitches列表的所有元素进行比较,然后打破循环.我的意思是它没有进入第二个p指数.输出是
1 1
1 2
1 3
1 4
它不会将controllerSwitches的第二个元素与allSwithces元素进行比较
IndexOutOfBoundException在比较结束时抛出.更改
for ( int w = 0; w <= allSwithces.size() ; w++)
Run Code Online (Sandbox Code Playgroud)
至
for ( int w = 0; w < allSwithces.size() ; w++)
Run Code Online (Sandbox Code Playgroud)
并打印所有组合.
你没有打破内循环.只是评论说它应该打破,但代码不会.
你continue是多余的,因为代码会自动继续.删除它们.
此外,放置完整的代码会有所帮助,因为您肯定会在调用者中收到异常.
| 归档时间: |
|
| 查看次数: |
1112 次 |
| 最近记录: |