Bill
Bill

Reputation: 3

How to find the index of an arraylist in an arraylist?

I have an ArrayList of ArrayLists of T, and I am trying to find the index of the ArrayList that contains a certain object

private int indexOfItem(T item)
{
    int index = 10000;

    for(int i = 0; i < bigList.size(); i++)
    {
        if(bigList.get(i).contains(item))
        {
            index = i;
        }

    }
    return index;
}

this works but is there a better way to do this using the indexOf() method that arrayLists have?

Upvotes: 0

Views: 1037

Answers (1)

f1sh
f1sh

Reputation: 11942

That is exactly what indexOf does with one exception: As soon as you find the item, you can stop looking:

private int indexOfItem(T item) {
  for(int i = 0; i < bigList.size(); i++) {
    if(bigList.get(i).contains(item)) {
      return i;
    }
  }
  return -1;
}

Upvotes: 1

Related Questions