I would like to try to create a generic method that takes an array, the length of the array, and an element that returns the position of the element in the array. I'm new to Java and I'm trying to learn Generics.
I can't seem to get the position of the element, and I don't even know if I'm doing this right, any advice would be great.
public class GenericsProj {
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4};
String[] stringArray = {"one", "two", "three", "four"};
routineArray(intArray, intArray.length, 4);
routineArray(stringArray, stringArray.length, "four");
}
public static <T> void routineArray(T[] array, T length, T element) {
System.out.println("Inside the array: " + Arrays.toString(array));
for (int i = 0; i < array.length; i++) {
if (array[i] == element) {
pos = array[i];
System.out.println(pos);
}
}
}
}