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;
}
I was thinking of a generic method where i pass the value and Constants into the method to get the required index of the string value. Its pretty much easy to do in JS
, but i am not sure how can i toss a method of same in java.
我在想一个泛型方法,我将值和常量传递给方法,以获得字符串值所需的索引。它在JS中很容易做到,但我不知道如何在java中抛出相同的方法。
CommonMethods.Utils.getIndex("Kevin", Contants.Name);
The above method is not working, as it says "length cannot be resolved or is not a field"
上面的方法不起作用,因为它说“长度无法解决或不是一个字段”
2 个解决方案
#1
3
Your Constants
object should be some sort of Collection
. Look those up. There's a lot of them with different attributes.
你的Constants对象应该是某种Collection。看那些。它们中有很多具有不同的属性。
What you seem to be rewriting here is a ArrayList
. Those already have a function to search inside called indexOf
.
你在这里重写的是ArrayList。那些已经具有搜索内部函数的函数indexOf。
#2
1
type
is a Constants
, not an array. You can't run type.length
because Constants
does not define a length
property.
type是常量,而不是数组。您不能运行type.length,因为常量不定义长度属性。
If Constants
is an enum you can use Constants.values()
to get an array of all constants.
如果Constants是一个枚举,你可以使用Constants.values()来获取所有常量的数组。
#1
3
Your Constants
object should be some sort of Collection
. Look those up. There's a lot of them with different attributes.
你的Constants对象应该是某种Collection。看那些。它们中有很多具有不同的属性。
What you seem to be rewriting here is a ArrayList
. Those already have a function to search inside called indexOf
.
你在这里重写的是ArrayList。那些已经具有搜索内部函数的函数indexOf。
#2
1
type
is a Constants
, not an array. You can't run type.length
because Constants
does not define a length
property.
type是常量,而不是数组。您不能运行type.length,因为常量不定义长度属性。
If Constants
is an enum you can use Constants.values()
to get an array of all constants.
如果Constants是一个枚举,你可以使用Constants.values()来获取所有常量的数组。