java lastindexof_Java中的Arraylist lastIndexOf() - Break易站

Java中的lastIndexOf()方法用于获取ArrayList对象中最后一次出现元素的索引。

句法 :

lastIndexOf(element)

参数:最后一个索引将被返回的元素。

返回:

它返回在参数中传递的元素的最后一次出现。如果找不到该元素,它将返回-1。

程序演示lastIndexOf():

// Java code to demonstrate the working of

// lastIndexOf() method in ArrayList

// for ArrayList functions

import java.util.ArrayList;

public class GFG {

public static void main(String[] args)

{

// creating an Empty Integer ArrayList

ArrayList arr = new ArrayList(7);

// using add() to initialize values

arr.add(10);

arr.add(20);

arr.add(30);

arr.add(40);

arr.add(30);

arr.add(30);

arr.add(40);

System.out.println("The list initially " + arr);

// last index of 30

int element = arr.lastIndexOf(30);

if (element != -1)

System.out.println("the lastIndexof of" +

" 30 is " + element);

else

System.out.println("30 is not present in" +

" the list");

// last index of 100

element = arr.lastIndexOf(100);

if (element != -1)

System.out.println("the lastIndexof of 100" +

" is " + element);

else

System.out.println("100 is not present in" +

" the list");

}

}

输出:

The list initially [10, 20, 30, 40, 30, 30, 40]

the lastIndexof of 30 is 5

100 is not present in the list

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值