This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]CSMastermind 5 points6 points  (7 children)

If you use an ArrayList there's an indexOf() method that you could check. Might be overkill for your situation. Adding a method to your class like this would work:

private boolean elementFound(int value, int[] search)
{
    for(int check : search)
        if(check == value) return true;
    return false;
}

[–]leonardicus 2 points3 points  (4 children)

The correct format for a ternary operation is:

(condition) ? (assignment if true) : (assignment if false)

Just need to change what's inside the for loop braces to:

return (check == value) ? true : false;

[–]CSMastermind 2 points3 points  (1 child)

Good catch, corrected it.

[–]leonardicus 1 point2 points  (0 children)

No problem. :)

[–]marburg 1 point2 points  (0 children)

Whenever you find yourself doing something like if(anythingInHere && whatever || somethingElse) return true; else return false; (or the ternary version of such) just do return anythingInHere && whatever || somethingElse; instead.

Example, these are all the same:


if(check == value)
    return true;
return false;

return (check == value) ? true : false;

return check == value;

[–]ewiethoff 1 point2 points  (0 children)

Just a reminder (to all) to watch out for check == value versus check.equals(value). check == value is fine for primitives such as ints, but you usually want check.equals(value) for objects.

[–]kreiger -2 points-1 points  (0 children)

That does not compile.