Java 实例 - List 截取


以下实例演示了如何使用 Collections 类的 indexOfSubList() 和 lastIndexOfSubList() 方法来查看子列表是否在列表中,并查看子列表在列表中所在的位置:

  1. /*
  2. author by shouce.ren
  3. Main.java
  4. */
  5.  
  6. import java.util.*;
  7.  
  8. public class Main {
  9. public static void main(String[] args) {
  10. List list = Arrays.asList("one Two three Four five six one three Four".split(" "));
  11. System.out.println("List :"+list);
  12. List sublist = Arrays.asList("three Four".split(" "));
  13. System.out.println("子列表 :"+sublist);
  14. System.out.println("indexOfSubList: "
  15. + Collections.indexOfSubList(list, sublist));
  16. System.out.println("lastIndexOfSubList: "
  17. + Collections.lastIndexOfSubList(list, sublist));
  18. }
  19. }

以上代码运行输出结果为:

  1. List :[one, Two, three, Four, five, six, one, three, Four]
  2. 子列表 :[three, Four]
  3. indexOfSubList: 2
  4. lastIndexOfSubList: 7