I need to send an integer array and a string array to this generic method and find out if a certain number or string is present there or not.I wrote this code but it is giving an error on the line if(e==30) saying that "Incompatible operand types E and int". Please help.
public class Ch2Lu3Ex2 { public static <E> void searchArray(E[] inputArray) { for(E e : inputArray) { if(e==30) { System.out.println("Element found in integer array"); } else if(e=="raj") { System.out.println("Element found in string array"); } } } public static void main(String[] args) { Integer[] integerArray = {10,20,30}; String[] stringArray = {"robin","raj","ravi"}; searchArray(integerArray); searchArray(stringArray); } }
==but withequals(), and secondly you don't need generics at all for this.