Java Collections lastIndexOfSubList()
lastIndexOfSubList() 用于获取指定源列表中最后一次出现的指定目标列表的起始位置。
1 语法
public static int lastIndexOfSubList(List<?> source, List<?> target)
2 参数
source:搜索目标的最后一次出现的列表。
target:作为源的subList搜索的列表。
3 返回值
返回在指定的目标列表的最后一个出现的起始位置的指定源列表中,或-1,如果不存在这样的发生。
4 Collections lastIndexOfSubList()示例1
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java Collections.lastIndexOfSubList的例子
*/
import java.util.*;
public class Demo {
public static void main(String[] args) {
//Create Source array list objects
List<String> srclist = new ArrayList<>();
//Add elements in the list
srclist.add("A");
srclist.add("B");
srclist.add("C");
srclist.add("D");
srclist.add("E");
//Create Target array list objects
List<String> targetlist = new ArrayList<>();
//Add elements in the list
targetlist.add("C");
targetlist.add("D");
targetlist.add("E");
//Check target list in source list
int index = Collections.lastIndexOfSubList(srclist, targetlist);
System.out.println("Target Output: "+index);
}
}
输出结果为:
Target Output: 2
5 Collections lastIndexOfSubList()示例2
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java Collections.lastIndexOfSubList的例子
*/
import java.util.*;
public class Demo {
public static void main(String[] args) {
//Create lists for source and destination
List<Integer> source = Arrays.asList(1,2,3);
List<Integer> target = Arrays.asList(4,5,6,7,8);
//Check target list in source list
int index = Collections.lastIndexOfSubList(target, source);
System.out.println("Target Output: "+index);
}
}
输出结果为:
Target Output: -1
6 Collections lastIndexOfSubList()示例3
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java Collections.lastIndexOfSubList的例子
*/
import java.util.*;
public class Demo {
public static void main(String[] args) {
//Creating array list with values
List<String> l1 = Arrays.asList("rose jack lucy eric paul mark".split(" "));
//Printing array list
System.out.println("List :" + l1);
//Sublist which we need IndexOf
List<String> sublist = Arrays.asList("eric paul".split(" "));
Integer Index = Collections.lastIndexOfSubList(l1, sublist);
//Printing Last IndexOfSublist
System.out.println("Last IndexOfSubList: " +Index);
}
}
输出结果为:
List :[rose, jack, lucy, eric, paul, mark]
Last IndexOfSubList: 3
热门文章
优秀文章