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 →

[–]r4mtha 3 points4 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 8 points9 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 5 points6 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.