public class Test6
{
public static void main(String[] args)
{
/* Integer[] numArr = {40,5,8,20,88,50,10,60};
Integer key = 4;
Integer index = getIndex(numArr,key);
System.out.println(Arrays.toString(numArr) + "中" + key + "位置在:" + index );
*/
String[] strArr = {"拉拉","嘻嘻","么么","嘎嘎","锤锤"};
String key = "锤锤";
int index = getIndex(strArr,key);
System.out.println(Arrays.toString(strArr) + "中" + key + "位置在:" + index );
}
public static <T> int getIndex(T[] arr, T key)
{
int index = -1;
for(int i=0; i<arr.length; i++)
{
if(!(arr[i].equals(key)))
continue;
else
{
index = i;
break;
}
}
return index;
}
}
真心觉得泛型很爽啊。。就是第一次定义有点难搞。如果真的搞懂了。真的是一次定义,多处使用。。 |