Better way to find index of item in ArrayList?

后端 未结 8 2056
不知归路
不知归路 2020-12-04 11:50

For an Android app, I have the following functionality

private ArrayList _categories; // eg [\"horses\",\"camels\"[,etc]]

private int getCateg         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 12:27

    ArrayList has a indexOf() method. Check the API for more, but here's how it works:

    private ArrayList _categories; // Initialize all this stuff
    
    private int getCategoryPos(String category) {
      return _categories.indexOf(category);
    }
    

    indexOf() will return exactly what your method returns, fast.

提交回复
热议问题