posted 11 years ago
Ok...I am having some trouble wrapping my head around the answer to a problem. Here it is:
// returns index of first occurrence of valid arr
// after position start;
// returns arr.length if val is not found
public int findNext(int[] arr, int val, int start)
{
int pos = start + 1;
while ((pos < arr.length) && (arr[pos] != val))
pos++;
return pos;
}
int[] arr = {11, 22, 100, 33, 100, 11, 44, 100};
System.out.println(findNext(arr, 100, 2));
The execution of the code results in the value 4 being printed. The way I see it, the result should be 3 right?
Also, why does the code not work if I change the condition to (arr[pos] != val) && (pos < arr.length)
Thanks for the help!!