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

时间:2016-04-04 18:45:27

标签: java 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?

1 个答案:

答案 0 :(得分:1)

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;
}
相关问题