查找一个ArrayList中的元素

import java.util.ArrayList;

public class SearchAnElement {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ArrayList arrayList = new ArrayList();
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");
        arrayList.add("4");
        arrayList.add("5");
        arrayList.add("1");
        arrayList.add("2");
     
        boolean blnFound = arrayList.contains("2");
        System.out.println("Does arrayList contain 2 ? " + blnFound);
     
        int index = arrayList.indexOf("4");
        if(index == -1)
          System.out.println("ArrayList does not contain 4");
        else
          System.out.println("ArrayList contains 4 at index :" + index);
         
        int lastIndex = arrayList.lastIndexOf("1");
        if(lastIndex == -1)
          System.out.println("ArrayList does not contain 1");
        else
          System.out.println("Last occurrence of 1 in ArrayList is at index :");
}

}

Console:
Does arrayList contain 2 ? true
ArrayList contains 4 at index :3
Last occurrence of 1 in ArrayList is at index :

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容