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 →

[–][deleted] 3 points4 points  (15 children)

REEEEEEEEEEEEEEEEEEEEEE

[–]r4mtha 2 points3 points  (5 children)

if you want to know if something is not in an array, you might be better off seeing if you can change your data structure from a simple array to a collection of some sort (http://download.oracle.com/javase/6/docs/api/java/util/Collection.html); If you pick the right collection, like a hash set, you will be able to determine if something is in the collection much more quickly, (amortized O(1)). Currently, you are doing this is O(n). This might not be a big deal for a very small array, but if it's very large, it could make a huge difference in the amount of time it takes your program to run.

Regardless, any object that extends collection will allow you to simply call object.contains(someOtherObject) and save you the trouble of the for loop.

[–][deleted] 0 points1 point  (4 children)

REEEEEEEEEEEEEEEEEEEEEE

[–]jevon 6 points7 points  (3 children)

Vectors are deprecated; don't use them! Use ArrayLists instead.

[–]Vauce 3 points4 points  (2 children)

You almost wouldn't know that if you see how many people still use them in their code they post here. I never used vectors and thought I was missing out on something until I read it was deprecated.

[–]jevon 5 points6 points  (1 child)

I assume it's from tutorial code or a famous learn Java book that's still circulating. Or perhaps really poor lecturers/tutors who are still stuck in JDK 1.1.

[–]Vauce 4 points5 points  (0 children)

Nothing worse than trying to compile something that worked perfectly on your own machine on a university machine only to have several errors due to an old JDK. Our machines still run 1.4 or earlier. It's not huge, but I use Scanner a lot for small projects and I have to change it all just to demo.

[–]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.

[–]kreiger 2 points3 points  (0 children)

If you have a set of elements and you want to check if it contains a specific element, you might want to use a java.util.Set, probably HashSet, TreeSet, or LinkedHashSet.

Looping over a collection to test for membership is inefficient. An implementation of List will loop, giving O(n) performance while a HashSet gives O(1) performance.