Better way to find index of item in ArrayList?

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

    the best solution here

    class Category(var Id: Int,var Name: String)
    arrayList is Category list
    val selectedPositon=arrayList.map { x->x.Id }.indexOf(Category_Id)
    spinner_update_categories.setSelection(selectedPositon)
    
    0 讨论(0)
  • 2020-12-04 12:36

    The best way to find the position of item in the list is by using Collections interface,

    Eg,

    List<Integer> sampleList = Arrays.asList(10,45,56,35,6,7);
    Collections.binarySearch(sampleList, 56);
    

    Output : 2

    0 讨论(0)
提交回复
热议问题