Java集合的lastlastIndexOfSubList()方法和示例
集合类lastIndexOfSubList()方法
lastIndexOfSubList()方法在java.util包中可用。
lastIndexOfSubList()方法用于返回给定(目标)列表在给定源列表(src)中最后一次出现的起始索引。
lastIndexOfSubList()方法是静态方法,因此可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。
在返回给定List(目标)的最后一次出现的索引时,lastIndexOfSubList()方法不会引发异常。
语法:
public static int lastIndexOfSubList(List src, List dest);
参数:
Listsrc–表示源列表,在其中过滤给定列表(目的地)的最后一次出现。
列表目标–表示要过滤给定源列表(src)的子列表的目标列表(目标)。
返回值:
此方法的返回类型为int,它返回给定源列表(src)中给定子列表(dest)的最后一次出现的起始索引,否则,当找不到搜索或列表为空时返回-1。
示例
//Java程序是演示示例
//lastIndexOfSubList()集合的int-
import java.util.*;
public class LastIndexOfSubList {
public static void main(String args[]) {
//实例化一个LinkedList-
List src_l = new LinkedList();
List dest_l = new LinkedList();
//通过使用add()方法是
//在链表src_l中添加元素
src_l.add(10);
src_l.add(20);
src_l.add(30);
src_l.add(40);
src_l.add(50);
//通过使用add()方法是
//在链接列表dest_l中添加元素
dest_l.add(40);
dest_l.add(50);
//显示LinkedList-
System.out.println("link_l: " + src_l);
System.out.println("dest_l: " + dest_l);
System.out.println();
//通过使用lastIndexOfSubList()方法是
//返回最后一次出现的起始索引
//src_l中的dest_l-
int index = Collections.lastIndexOfSubList(src_l, dest_l);
//显示索引
System.out.println("Collections.lastIndexOfSubList(src_l,dest_l): " + index);
}
}输出结果
link_l: [10, 20, 30, 40, 50] dest_l: [40, 50] Collections.lastIndexOfSubList(src_l,dest_l): 3