I have an array
int[] arr = {9,20,-2,-45,23,5,1};
I’m sorting it using
java.util.Arrays.sort(arr);
The for loop:
for(int i =0;(i<arr.length) && (arr[i] > 0) ; i++)
doesn’t seem to work because of the condition (arr[i] > 0). I coudn’t think of a reason why this shouldn’t work. What’s surprising is that the follwing loop works in expected manner :
for(int i =0;(i<arr.length) && (arr[i] != 0) ; i++)
and
for(int i =0;(i<arr.length) && (arr[i] < 0) ; i++)
java.util.Arrays.sort(int[] arg);sorts in ascending order, as a result the first value (arr[0]) after sorting would be -45.for(int i =0;(i<arr.length) && (arr[i] > 0) ; i++)would be false on the first iteration sincearr[0] == -45, and therefor(arr[0] > 0)is false.)