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


Java CopyOnWriteArrayList lastIndexOf(E e, int index)用法及代码示例


Java CopyOnWriteArrayList 类的 lastIndexOf(E e, int index) 方法返回此列表中指定元素最后一次出现的索引,该索引从索引向后搜索。如果未找到该元素,则此方法将返回 -1。

用法:

public int lastIndexOf(E e, int index)

参数:

e- 要搜索的元素

index- 开始向后搜索的索引

返回值:

此方法返回元素在此列表中小于或等于 index 的位置最后一次出现的索引。如果未找到该元素,则此方法返回 -1。

例子1

import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteArrayListLastIndexOfExample1{
	
	public static void main(String[] args) {
		//Initializing CopyOnWriteArrayList of Integers
		CopyOnWriteArrayList<Integer>numbers = new CopyOnWriteArrayList<>(new Integer[] {1,2,1,4,5,3,3,8});
		//Prints the index of the last occurrence of the specified element in this list, searching backwards from index, or returns -1 if the element is not found.
		System.out.println("Last Index of 3 is:" + numbers.lastIndexOf(3, 7));	
			}
	}

输出:

Last Index of 3 is:6

例子2

import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteArrayListLastIndexOfExample2{
	
	public static void main(String[] args) {
		//Initializing CopyOnWriteArrayList of Integers
		CopyOnWriteArrayList<Integer>numbers = new CopyOnWriteArrayList<>(new Integer[] {1,2,1,4,5,3,3,8});
		//Prints the index of the last occurrence of the specified element in this list, searching backwards from index, or returns -1 if the element is not found.
		System.out.println("Last Index of 3 is:" + numbers.lastIndexOf(11, 7));
			}
		}

输出:

Last Index of 11 is:-1

例子3

import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteArrayListLastIndexOfExample3{
	
	public static void main(String[] args) {
		//Initializing CopyOnWriteArrayList of Strings
		CopyOnWriteArrayList<String>listOfNames = new CopyOnWriteArrayList<>(new String[] {"Tom","Harry","John","John","John","Jerry"});
		//Prints the index of the last occurrence of the specified element in this list, searching backwards from index, or returns -1 if the element is not found.
		System.out.println("Last Index of John is:" + listOfNames.lastIndexOf("John", 5));
			}
		}

输出:

Last Index of John is:4

示例 4

import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteArrayListLastIndexOfExample4{
	
	public static void main(String[] args) {
		//Initializing CopyOnWriteArrayList of Strings
		CopyOnWriteArrayList<String>listOfNames = new CopyOnWriteArrayList<>(new String[] {"Tom","Harry","John","John","John","Jerry"});
		//Prints the index of the last occurrence of the specified element in this list, searching backwards from index, or returns -1 if the element is not found.
		System.out.println("Last Index of Lincoln is:" + listOfNames.lastIndexOf("Lincoln", 5));	
			}	
	}

输出:

Last Index of Lincoln is:-1






相关用法


注:本文由纯净天空筛选整理自 Java CopyOnWriteArrayList lastIndexOf(E e, int index) Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。