I have a problem that I'm stuck on. Namely, I have a for loop that goes through an integer array and compares it to another integer. If any integer in the array is the same as the given integer it's true, otherwise, it's false.
public static boolean search(int item, int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == item) {
return true;
}
}
return false;
}
What I'm wondering is how could I modify this code so that I could input any type of item and array (ie String or int or double, etc) and have it do the same thing. I attempted to do something like:
public static boolean search(Object item, Object[] arr) {
for (int i = 0; i < arr.length; i++) {
if (arr[i].equals(item)) {
return true;
}
}
return false;
}
However this doesn't work for ints. If possible, could you keep this at a more conceptual level rather than straight giving me the answer as I would like to code and debug it myself. Conceptually I just want to know what is the generic form of everything (int, String, etc).
Thanks!