Get index of contain sublist from list java

前端 未结 3 1741
小鲜肉
小鲜肉 2020-12-19 05:54

I have string lists look like this:

 List parentDataList:  {\"this\", \"is\", \"a\", \"test\", \"string\", \"and\", \"a\", \"test\", \"other\"}         


        
3条回答
  •  离开以前
    2020-12-19 06:36

    The method Collections.indexOfSubList will give you the desired information.

    Returns the starting position of the first occurrence of the specified target list within the specified source list, or -1 if there is no such occurrence. More formally, returns the lowest index i such that source.subList(i, i+target.size()).equals(target), or -1 if there is no such index. (Returns -1 if target.size() > source.size().)

    int index=Collections.indexOfSubList(parentDataList, child1);
    …
    

    The index interval will be from index, inclusive, to index+child1.size(), exclusive. Unless the returned index is -1, of course. In the latter case the sublist was not found.

提交回复
热议问题