By:Mohammad-Ali Bandzar | May 9 2019
An introduction to Searching with java
If you have ever wanted to know if an item is present in an array or know its position(index) in an array, you're in luck because java has a built in method to do just that:
VariableName.indexOf(SearchingFor);
This metod will return the first position(index) of the item. something you should be aware of is that if there are multiple matches within the array it will only return the location of the first one as this method stops searching after it finds a match. The method will also return a location of -1 if no match is found. If you are instead intrested in writing your own search algorithm read on.
This is the simplist search to understand and operates just like the one used by the intbuilt method mentioned above, it goes through every element in the array in order and stops when a match is found and returns its position. if no match is found, -1 is returned.
public int sequentialSearch(Object [ ] arr, Object value){ for (int i = 0; i < arr.length ; i++){ if (value.equals(arr [i])){ return i; } } return -1; }
THANKS FOR READING
credits:https://www.vogella.com/tutorials/JavaAlgorithmsSearch/article.html