Reputation: 3
public static int search(int[] a, int target)
{
int i=0;
boolean found = false;
while((i<a.length) && ! found)
{
if (a[i] == target)
{
found = true;
}
else i++;
}
if (found) return i;
else return -1;
}
I dont understand the if statement part. So how i am reading it in my head is found is set to false. If not found...so if not false (since found = false), do whatever. So basically im reading it as a double negative and seeing if (true) dow whatever. But it doesnt make sense. I know its not an inifite loop and it runs fine but I dont get the logic, it must not be a double negative. Thanks!
EDIT: So i get that we could just return i, much easier yes I agree. I just am having trouble with the logic of the boolean value being used in the loop with the not "!" symbol.
Basically if i wrote this i would say (ignoring everything else)
found = true //found is true to begin with while (!found) //while not true continue to next index //continue until ....actually i'm getting very confused now because to break the loop we would continue until found is false which logically is backwards
EDIT: Thank you everyone for your comments!! It all helped me understand it!
Upvotes: 0
Views: 3752
Reputation: 1043
What you think about using IntStream ?
public static int search(int[] a, int target) {
final OptionalInt index = IntStream.range(0, a.length).filter(i -> a[i] == target).findFirst();
return index.isPresent() ? index.getAsInt() : -1;
}
Upvotes: 0
Reputation: 612
Set the boolean found = false;
because you have not found what you're looking for yet, then while
(i<a.length)
and ! found
means that if you have not reached the end of the array and you have not found what you're looking for do
{
if (a[i] == target)
{
found = true;
}
else i++;
}
If you found it change the variable found
to true which with the negation operator !
will be changed to false and the &&
will take you out of the while loop since one of the 2 conditions is not true any more.
In other words if found
is false then in the loop is true because you have !
(not) found it. And if found
is true then in the loop is false because you have found it.
Upvotes: 0
Reputation: 31
So, I think your doubt is on the while statement, but I'll put some comments on all the code to try to explain what it does:
public static int search(int[] a, int target)
{
int i=0;
boolean found = false;
// While i index is less than the array length AND item hasn't been found (negation of the found variable), keep iterating on the loop. If not (if any of this two conditions aren't met), continue.
while((i<a.length) && ! found)
{
/* If the targeted item has been found, set found boolean to true, so this loop will stop before next iteration
*/
if (a[i] == target)
{
found = true;
}
// If targeted item is not this one, increase the index for next iteration
else i++;
}
/* This condition only will be met if we exit the loop because we found the target. In that case, we will return the index in the array of that index. If we "finished" the array without finding it, we will return -1, meaning "it wasn't found".
*/
if (found) return i;
else return -1;
}
Upvotes: 0
Reputation: 1815
public static int search(int[] a, int target)
{
for (int i = 0; i < a.length; i++){
if (a[i] == target) return a[i]; // or i if you want to get ingex of searched element
}
return -1;
}
Upvotes: 2
Reputation: 99
At the start of the loop found is false, so the while loop will start. Once a value is found, found is set to true and the loop stops. I would rewrite the loop however:
public static int search( int[] a, int target)
{
int foundIdx = -1;
for( int i = 0; i < a.length && foundIdx < 0; i++ )
{
if( a[i] == target )
{
foundIdx = i;
break;
}
}
return foundIdx;
}
Upvotes: 0
Reputation: 921
Let me break it down into a few parts for you:
while((i<a.length) && ! found)
So you start with false. As !false
evaluates to true, you continue the loop until found
is true
which will make the whole condition false, causing you to break the loop.
i<a.length
: while i<length
returns true
, continue the loop.
You basically want found
to be false and i<length
for the loop to continue. If any of the condition isn't met, you break the loop (check out the &&
).
if (a[i] == target) found = true;
else i++;
This is simple: if the number is found, make the found boolean true. This will cause the next iteration condition to evaluate as false, causing the loop to break.
Upvotes: 1
Reputation: 31
Do you think it is easier?
public static int search(int[] a, int target) {
for (int i = 0; i < a.length; i++) {
if (a[i] == target) {
return i;
}
}
return -1;
}
Upvotes: 1
Reputation: 727047
!found
means the same thing that it means in English, i.e. "not found". The entire condition with i<a.length
reads "while i
is a valid index and [target is] not found", which is pretty close, given that you know that "not found" refers to target
You can simplify this loop to avoid Boolean variable:
while(i < a.length) {
if (a[i] == target) {
return i;
}
i++;
}
return -1;
Upvotes: 1
Reputation: 6532
The code loops over the array of int
s while found
is false
.
During the loop it compares the current array value in a (a[i]
) to the target, If the values are the same, it sets found
to be true
.
Thus after the loop it checks if found
is true
, and if it is it returns i
, the index of the last comparison value.
Upvotes: 0
Reputation: 737
The ! operator inverts the following logical statement (logical not gate).
Because !false is true, it means it is not a double negative.
Upvotes: 1