I am trying to search an array of any data type (Int, Strings, Chars, etc...) to see if there exist an element that matches the one you input. You should return the index of the matching element. There are two classes being used.
The error I get is:
"Cannot make a static reference to the non-static method find(Object[], Object) from the type ArraySearch"
Its suggestion is to make the method static, however, doing that gives me an error in the Search class:
"Cannot make a static reference to the non-static type E".
Search Class:
public class ArraySearch<E> {
public int find (E[] array, E item) {
int index = 0;
for (int i = 0; i < array.length; i++) {
if (array[i].equals(item)) {
System.out.println("There is a element " + array[i] +
" at index " + i);
index = i;
break;
}
}
return index;
}
}
Runner Class:
public class ArraySearchRunner {
public static void main(String[] args) {
String[] strings = new String[]{"Jim", "Tim", "Bob", "Greg"};
Integer[] ints = new Integer[]{1, 2, 3, 4, 5};
ArraySearch.find(strings, "Bob");
ArraySearch.find(ints, 4);
}
}
What is the best solution in this case?
Thanks,
contains(). ;-)contains()method for arrays. But I see what you are talking about.Arrays.asList( a ).contains( e )