public static int getIndex(String value, Constants type) {
int myPosition = -1;
for (int i = 0; i < type.length; i++) {
if (type[i] == value) {
myPosition = i;
break;
}
}
return myPosition;
}
我想到了一種通用方法,將值和常量傳遞給方法以獲取字符串值所需的索引。它在JS
很容易做到,但我不知道我怎麼能在java中拋棄相同的方法。獲取傳入的值的索引
CommonMethods.Utils.getIndex("Kevin", Contants.Name);
上述方法不工作,因爲它說「長度不能得到解決或不是一個場」
什麼是「常量」?一個枚舉?一個界面?一類? 「length」屬性在數組上是有效的(當在類或接口上顯式聲明「length」時)。 – rgettman 2013-05-08 22:52:22
可能的重複[如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – jlordo 2013-05-08 22:53:42
代碼應該做什麼? – wchargin 2013-05-08 22:54:41