for loop in below java code stop in the first iteration, I don’t know why! can you help me?
controllerSwitches elements is { 1, 2}
allSwithces elements is {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;
}
it gets the first p index and compare it with all element of allSwitches list, then its break the loop. I mean it doesn’t go to the second p index. the output is
1 1
1 2
1 3
1 4
it does not compare the second element of controllerSwitches with allSwithces elements
w <= allSwithces.size()in your inner for loop? Some kind of IndexOutOfBounds maybe? No?continuestatements?continuestatements are not inside of a conditional block, so they're definitely going to be hit on the first loop every time.continuedoesn't mean "do the next iteration of the loop", it means "stop this loop and continue at the next instruction after the loop"breakandcontinuemixed up.for(int i=0; i<10; i++) {System.out.println(i);continue;}will output 0 through 9.