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

前端 未结 13 2072
谎友^
谎友^ 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:41

    Refactoring the above methods and showing with the use:

    private String[] languages = {"pt", "en", "es"};
    private Integer indexOf(String[] arr, String str){
       for (int i = 0; i < arr.length; i++)
          if(arr[i].equals(str)) return i;
       return -1;
    }
    indexOf(languages, "en")
    

提交回复
热议问题