行业资讯 2025年08月6日
0 收藏 0 点赞 716 浏览 811 个字
摘要 :

文章目录 1. ArrayList.lastIndexOf() API 2. ArrayList lastIndexOf()示例 了解如何使用 ArrayList.lastIndexOf() 方法获取 ArrayList 中元素最后一次出现的索引。若……




  • 1. ArrayList.lastIndexOf() API
  • 2. ArrayList lastIndexOf()示例

了解如何使用 ArrayList.lastIndexOf() 方法获取 ArrayList 中元素最后一次出现的索引。若要获取第一次出现的索引,请使用 indexOf() 方法。

1. ArrayList.lastIndexOf() API

lastIndexOf(e) 返回此列表中指定元素 e 最后一次出现的索引。如果列表不包含元素,它将返回 -1 

public int lastIndexOf(Object object)

lastIndexOf() 只接受一个参数对象,需要在列表中搜索其最后一个索引位置。

  • index – 如果找到元素,则元素的最后一个索引位置。
  • -1 – 如果未找到该元素。

2. ArrayList lastIndexOf()示例

以下 Java 程序获取 ArrayList 的最后一个索引。在此示例中,我们寻找字符串 “alex” 和 “hello” 的最后一次出现。

  • 字符串 “alex” 在列表中出现三次,第二次出现在索引位置 6。
  • 字符串 “hello” 不在列表中。

请注意,ArrayList 的索引从 0 开始。

ArrayList<String> list = new ArrayList<>(Arrays.asList(\"alex\", \"brian\", \"charles\",\"alex\",\"dough\",\"gary\",\"alex\",\"harry\"));
int lastIndex = list.lastIndexOf(\"alex\");
System.out.println(lastIndex);
lastIndex = list.lastIndexOf(\"hello\");
System.out.println(lastIndex);

程序输出:

6
-1

以上就是Java Arraylist.lastIndexOf()方法详解的全部内容。

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/9303.html

管理员

相关推荐
2025-08-06

文章目录 一、Reader 接口概述 1.1 什么是 Reader 接口? 1.2 Reader 与 InputStream 的区别 1.3 …

988
2025-08-06

文章目录 一、事件溯源 (一)核心概念 (二)Kafka与Golang的优势 (三)完整代码实现 二、命令…

465
2025-08-06

文章目录 一、证明GC期间执行native函数的线程仍在运行 二、native线程操作Java对象的影响及处理方…

348
2025-08-06

文章目录 一、事务基础概念 二、MyBatis事务管理机制 (一)JDBC原生事务管理(JdbcTransaction)…

456
2025-08-06

文章目录 一、SnowFlake算法核心原理 二、SnowFlake算法工作流程详解 三、SnowFlake算法的Java代码…

517
2025-08-06

文章目录 一、本地Jar包的加载操作 二、本地Class的加载方法 三、远程Jar包的加载方式 你知道Groo…

832
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号