Reputation: 368
Why does
int row = 1;
// multiArray[col].length = 6
while(multiArray[col][row] > 1 && row < multiArray[col].length) {
sum += multiArray[col][row];
row++;
}
Return an IndexOutOfBoundsException?
I thought that if row became 6, the while loop just wouldn't run, instead of returning an error.
Upvotes: 0
Views: 24
Reputation: 178263
The &&
operator is a short-circuit operator. That means that if it can tell the results from the left operand, then it won't evaluate the right side.
Switch the order of operands, so the in-bounds check comes first, and if it fails, the array value check won't occur, which would throw the exception if evaluated.
while(row < multiArray[col].length && multiArray[col][row] > 1) {
Upvotes: 2