Better way to find index of item in ArrayList?

后端 未结 8 2055
不知归路
不知归路 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:17

    There is indeed a fancy shmancy native function in java you should leverage.

    ArrayList has an instance method called

    indexOf(Object o)

    (http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html)

    You would be able to call it on _categories as follows:

    _categories.indexOf("camels")

    I have no experience with programming for Android - but this would work for a standard Java application.

    Good luck.

提交回复
热议问题