Better way to find index of item in ArrayList?

278,418

Solution 1

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

private ArrayList<String> _categories; // Initialize all this stuff

private int getCategoryPos(String category) {
  return _categories.indexOf(category);
}

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

Solution 2

ArrayList<String> alphabetList = new ArrayList<String>();
alphabetList.add("A"); // 0 index
alphabetList.add("B"); // 1 index
alphabetList.add("C"); // 2 index
alphabetList.add("D"); // 3 index
alphabetList.add("E"); // 4 index
alphabetList.add("F"); // 5 index
alphabetList.add("G"); // 6 index
alphabetList.add("H"); // 7 index
alphabetList.add("I"); // 8 index

int position = -1;
position = alphabetList.indexOf("H");
if (position == -1) {
    Log.e(TAG, "Object not found in List");
} else {
    Log.i(TAG, "" + position);
}

Output: List Index : 7

If you pass H it will return 7, if you pass J it will return -1 as we defined default value to -1.

Done

Solution 3

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).

Solution 4

Java API specifies two methods you could use: indexOf(Object obj) and lastIndexOf(Object obj). The first one returns the index of the element if found, -1 otherwise. The second one returns the last index, that would be like searching the list backwards.

Solution 5

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.

Share:
278,418
Jacksonkr
Author by

Jacksonkr

Another guy in a chair with questions &amp; answers. jacksonkr.com

Updated on November 09, 2020

Comments

  • Jacksonkr
    Jacksonkr over 3 years

    For an Android app, I have the following functionality

    private ArrayList<String> _categories; // eg ["horses","camels"[,etc]]
    
    private int getCategoryPos(String category) {
        for(int i = 0; i < this._categories.size(); ++i) {
            if(this._categories.get(i) == category) return i;
        }
    
        return -1;
    }
    

    Is that the "best" way to write a function for getting an element's position? Or is there a fancy shmancy native function in java the I should leverage?

  • Hunter McMillen
    Hunter McMillen over 12 years
    native function implies C\C++ to me..Just saying.
  • Admin
    Admin over 12 years
    It is no "fast"er in terms of complexity than the code posted, although it may be implemented more efficiently. Also, indexOf will react slightly differently here: the original code [incorrectly] uses == while indexOf uses equals().
  • yshavit
    yshavit over 12 years
    In fact, it's almost the same code exactly (at least in the Sun Java 6 code I have), except that they start it with an if-else branch that handles null separately.
  • boctulus
    boctulus over 9 years
    It's wired old arrays and List<> have FindIndex() method but the API changes in the middle for ArrayList :D
  • Jimale Abdi
    Jimale Abdi about 5 years
    How i can make the vise versa of this, for your solution your input is H and you get the position of H, Suppose if my input is the Index of 7 how i can get the the String value of that index. Thank You
  • Hiren Patel
    Hiren Patel about 5 years
    @JimaleAbdi Do yourArrayList.get(7). 7 is your position.