I don't understand the reason why the follow java code throws a ArrayIndexOutofBound exception
while (intArray[j] < intArray[j - 1] && j > 0) {
int temp = intArray[j - 1];
intArray[j - 1] = intArray[j];
intArray[j] = temp;
j--;
}
It appears that even if I AND(&&) both conditions, it will execute one of the conditions first to check if it qualifies the condition, in this case, it will be intArray[0] < intArray[-1] and cause out of bound exception. Which confuses me at first, now I just wandering, why does the program behave that way?
Aucun commentaire:
Enregistrer un commentaire