Does Java have a built-function to allow me to linearly search for an element in an array or do I have to just use a for loop?
In short, to find elements in an array you should: Create a String array. Use contains(Object[] array, Object objectToFind) method of ArrayUtils to check if the object is in the given array. Use the indexOf(Object[] array, Object objectToFind) method find the index of the given object in the array.
The isArray() method of Class is used to check whether an object is an array or not. The method returns true if the given object is an array. Otherwise, it returns false .
There is a contains
method for lists, so you should be able to do:
Arrays.asList(yourArray).contains(yourObject);
Warning: this might not do what you (or I) expect, see Tom's comment below.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With