How to find index of STRING array in Java from a given value?

前端 未结 13 2110
谎友^
谎友^ 2020-12-08 02:11

I wanted to know if there\'s a native method in array for Java to get the index of the table for a given value ?

Let\'s say my table contains these strings :

<
13条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 02:52

    for (int i = 0; i < Types.length; i++) {
        if(TYPES[i].equals(userString)){
            return i;
        }
    }
    return -1;//not found
    

    You can do this too:

    return Arrays.asList(Types).indexOf(userSTring);
    

提交回复
热议问题