Better way to find index of item in ArrayList?

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

    If your List is sorted and has good random access (as ArrayList does), you should look into Collections.binarySearch. Otherwise, you should use List.indexOf, as others have pointed out.

    But your algorithm is sound, fwiw (other than the == others have pointed out).

提交回复
热议问题