Tuesday 2 April 2013

Search Elements in Array List

// ------ Search elements in ArrayList ---------------- 


package arraylist;

import java.util.ArrayList;

public class SearchAnElementInArrayListExample {
public static void main(String args[]){
ArrayList arraylist= new ArrayList();
arraylist.add("5");
arraylist.add("10");
arraylist.add("1");
arraylist.add("4");
arraylist.add("8");
arraylist.add("3");
arraylist.add("9");
arraylist.add("2");
boolean found = arraylist.contains("40");
System.out.println("Does ArrayList contains 4 "+found);
int index = arraylist.indexOf("80");
if(index == -1)
System.out.println("element 80 not found...");
else
System.out.println("element 80 found...");
int lastindex = arraylist.lastIndexOf("9");
if(lastindex== -1)
System.out.println("element 9 not exist...");
else
System.out.println("element 9 is exist");
}

}

No comments:

Post a Comment