首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在ArrayList中搜索String实例

,可以使用ArrayList的indexOf()方法来实现。该方法会返回指定元素在ArrayList中的索引位置,如果找不到则返回-1。

下面是一个完整的示例代码:

代码语言:txt
复制
import java.util.ArrayList;

public class SearchStringInArrayList {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("apple");
        arrayList.add("banana");
        arrayList.add("orange");
        arrayList.add("grape");

        String searchString = "banana";
        int index = arrayList.indexOf(searchString);

        if (index != -1) {
            System.out.println("String found at index: " + index);
        } else {
            System.out.println("String not found in the ArrayList.");
        }
    }
}

在上述代码中,我们创建了一个ArrayList对象,并添加了一些String实例。然后,我们定义了一个要搜索的字符串searchString,并使用indexOf()方法在ArrayList中搜索该字符串。如果找到了该字符串,就打印出它在ArrayList中的索引位置;如果没有找到,则打印出"String not found in the ArrayList."。

腾讯云相关产品和产品介绍链接地址:

  • 云服务器(CVM):https://cloud.tencent.com/product/cvm
  • 云数据库 MySQL 版(CDB):https://cloud.tencent.com/product/cdb
  • 云原生容器服务(TKE):https://cloud.tencent.com/product/tke
  • 人工智能平台(AI Lab):https://cloud.tencent.com/product/ailab
  • 物联网开发平台(IoT Explorer):https://cloud.tencent.com/product/iothub
  • 移动推送服务(信鸽):https://cloud.tencent.com/product/tpns
  • 对象存储(COS):https://cloud.tencent.com/product/cos
  • 区块链服务(BCS):https://cloud.tencent.com/product/bcs
  • 腾讯云游戏引擎(GSE):https://cloud.tencent.com/product/gse

请注意,以上链接仅供参考,具体产品选择应根据实际需求进行评估。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

24分47秒

1.尚硅谷全套JAVA教程--基础必备(67.32GB)/尚硅谷Java入门教程,java电子书+Java面试真题(2023新版)/08_授课视频/169-数据结构与集合源码-ArrayList在JDK7和JDK8中的源码剖析.mp4

5分53秒

Elastic 5分钟教程:使用跨集群搜索解决数据异地问题

8分46秒

【玩转腾讯云】初次体验腾讯云分布式数据库TDSQL

2分59秒

Elastic 5分钟教程:使用机器学习,自动化异常检测

2分17秒

Elastic 5分钟教程:使用Logs应用搜索你的日志

-

亲测!微信电脑端可以刷朋友圈了,网友:上班能光明正大摸鱼了

2分43秒

ELSER 与 Q&A 模型配合使用的快速演示

9分19秒

036.go的结构体定义

领券