package com.javacodegeeks.snippets.core;
import java.util.ArrayList;
public class SearchElementsInArrayList {
public static void main(String[] args) {
// Create an ArrayList and populate it with elements
ArrayList arrayList = new ArrayList();
arrayList.add("element_1");
arrayList.add("element_2");
arrayList.add("element_3");
arrayList.add("element_1");
/*
*
* boolean contains(Object element) operation returns true
*
* if the ArrayList contains the specified object, false otherwise.
*/
boolean found = arrayList.contains("element_2");
System.out.println("Found element_2 : " + found);
/*
*
* int indexOf(Object element) operation returns the index of the
*
* first occurance of the specified element in ArrayList or -1 if
*
* the specific element is not found. To get the index of the last
*
* occurance of the specified element in ArrayList use the
*
* int lastIndexOf(Object element) operation instead.
*/
int index = arrayList.indexOf("element_3");
System.out.println("Found element_3 : " + (index == -1 ? false : true)
+ ", in position : " + index);
int lastIndex = arrayList.lastIndexOf("element_1");
System.out.println("Found element_1 : "
+ (lastIndex == -1 ? false : true) + ", in position : "
+ lastIndex);
}
}
原文:
http://examples.javacodegeeks.com/core-java/util/arraylist/search-elements-in-arraylist-example/
查找ArrayList元素的下标
最新推荐文章于 2024-08-21 03:43:51 发布