当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Collections indexOfSubList()用法及代码示例


java.util.Collections类的indexOfSubList()方法用于返回指定源列表中指定目标列表第一次出现的起始位置,如果没有出现,则返回-1。更正式地,返回最低索引i,以使source.subList(i,i + target.size()).equals(target)或-1(如果没有这样的索引)。 (如果target.size()> source.size(),则返回-1。)

此实现使用“brute force”技术扫描源列表,依次在每个位置查找与目标的匹配项。

用法:


public static int indexOfSubList(List source, List target)

参数:该方法将以下参数作为参数

  • source –在其中搜索首次出现的目标的列表。
  • target –要搜索的列表作为源的子列表。
  • 返回值:此方法返回指定源列表中指定目标列表第一次出现的起始位置,如果没有出现,则返回-1。

    以下示例说明了indexOfSubList()方法

    示例1:

    // Java program to demonstrate 
    // indexOfSubList() method 
    // for String value 
      
    import java.util.*; 
      
    public class GFG1 { 
        public static void main(String[] argv) 
            throws Exception 
        { 
            try { 
      
                // creating object of List<String> 
                List<String> arrlistsrc = new ArrayList<String>(); 
                List<String> arrlisttarget = new ArrayList<String>(); 
      
                // Adding element to arrlistsrc 
                arrlistsrc.add("A"); 
                arrlistsrc.add("B"); 
                arrlistsrc.add("C"); 
                arrlistsrc.add("D"); 
                arrlistsrc.add("E"); 
      
                // Adding element to arrlisttarget 
                arrlisttarget.add("C"); 
                arrlisttarget.add("D"); 
                arrlisttarget.add("E"); 
      
                // print the source list 
                System.out.println("Source list: " + arrlistsrc); 
      
                // print the target list 
                System.out.println("Target list: " + arrlisttarget); 
      
                // check target list in source list 
                int index = Collections 
                                .indexOfSubList(arrlistsrc, 
                                                arrlisttarget); 
      
                // print the index 
                System.out.println("Target list starts at index: "
                                   + index); 
            } 
      
            catch (IllegalArgumentException e) { 
                System.out.println("Exception thrown : " + e); 
            } 
        } 
    }
    输出:
    Source list: [A, B, C, D, E]
    Target list: [C, D, E]
    Target list starts at index: 2
    

    示例2:

    // Java program to demonstrate 
    // indexOfSubList() method 
    // for Integer value 
      
    import java.util.*; 
      
    public class GFG1 { 
        public static void main(String[] argv) 
            throws Exception 
        { 
            try { 
      
                // creating object of List<Integer> 
                List<Integer> arrlistsrc = new ArrayList<Integer>(); 
                List<Integer> arrlisttarget = new ArrayList<Integer>(); 
      
                // Adding element to arrlistsrc 
                arrlistsrc.add(20); 
                arrlistsrc.add(30); 
                arrlistsrc.add(40); 
                arrlistsrc.add(50); 
                arrlistsrc.add(60); 
      
                // Adding element to arrlisttarget 
                arrlisttarget.add(40); 
                arrlisttarget.add(50); 
      
                // print the source list 
                System.out.println("Source list: " + arrlistsrc); 
      
                // print the target list 
                System.out.println("Target list: " + arrlisttarget); 
      
                // check target list in source list 
                int index = Collections 
                                .indexOfSubList(arrlistsrc, 
                                                arrlisttarget); 
      
                // print the index 
                System.out.println("Target list starts at index: "
                                   + index); 
            } 
      
            catch (IllegalArgumentException e) { 
                System.out.println("Exception thrown : " + e); 
            } 
        } 
    }
    输出:
    Source list: [20, 30, 40, 50, 60]
    Target list: [40, 50]
    Target list starts at index: 2
    


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Collections indexOfSubList() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。