简体   繁体   English

如何在数组列表中找到数组列表的索引?

[英]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 我有一个T的ArrayList的ArrayList,并且我试图找到包含某个对象的ArrayList的索引

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? 这可行,但是有没有更好的方法使用arrayLists拥有的indexOf()方法呢?

That is exactly what indexOf does with one exception: As soon as you find the item, you can stop looking: 这正是indexOf但有一个例外:一旦找到该项目,就可以停止寻找:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM