入力した要素と一致する要素が存在するかどうかを確認するために、任意のデータ型 (Int、Strings、Chars など) の配列を検索しようとしています。一致する要素のインデックスを返す必要があります。2つのクラスが使用されています。
私が得るエラーは次のとおりです。
"Cannot make a static reference to the non-static method find(Object[], Object) from the type ArraySearch"
その提案は、メソッドを静的にすることですが、それを行うと Search クラスでエラーが発生します。
"Cannot make a static reference to the non-static type E".
検索クラス:
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;
}
}
ランナークラス:
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);
}
}
この場合の最善の解決策は何ですか?
ありがとう、