我需要發送一個整型數組和字符串數組,這個通用的方法,並找出如果某個數字或字符串存在有或not.I寫了這個代碼,但它給了一個錯誤就行了if(e==30)
說那「Incompatible operand types E and int
」。請幫忙。泛型方法
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);
}
}
有很多事情錯在這裏,他們是,你應該(幾乎)從不使用''==但'等於()'比較對象的拳頭,其次你並不需要仿製藥在所有的這個。 – biziclop