📜  Java中的集合 indexOfSubList() 方法和示例

📅  最后修改于: 2022-05-13 01:54:27.330000             🧑  作者: Mango

Java中的集合 indexOfSubList() 方法和示例

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

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

句法:

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
                List arrlistsrc = new ArrayList();
                List arrlisttarget = new ArrayList();
      
                // 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
                List arrlistsrc = new ArrayList();
                List arrlisttarget = new ArrayList();
      
                // 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