1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/* Search an element of Java ArrayList Example This Java Example shows how to search an element of java ArrayList object using contains, indexOf and lastIndexOf methods. */ import java.util.ArrayList; public class SearchAnElementInArrayListExample { public static void main(String[] args) { //create an ArrayList object ArrayList arrayList = new ArrayList(); //Add elements to Arraylist arrayList.add("1"); arrayList.add("2"); arrayList.add("3"); arrayList.add("4"); arrayList.add("5"); arrayList.add("1"); arrayList.add("2"); /* To check whether the specified element exists in Java ArrayList use boolean contains(Object element) method. It returns true if the ArrayList contains the specified objct, false otherwise. */ boolean blnFound = arrayList.contains("2"); System.out.println("Does arrayList contain 2 ? " + blnFound); /* To get an index of specified element in ArrayList use int indexOf(Object element) method. This method returns the index of the specified element in ArrayList. It returns -1 if not found. */ 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); /* To get last index of specified element in ArrayList use int lastIndexOf(Object element) method. This method returns index of the last occurrence of the specified element in ArrayList. It returns -1 if not found. */ 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 :" + lastIndex); } } /* Output would be Does arrayList contain 2 ? true ArrayList contains 4 at index :3 Last occurrence of 1 in ArrayList is at index :5 */ |
[code]
/*
Check if subset contains subset arraylist
*/
import java.util.ArrayList;
public class Test {
public static void main(String[] args){
ArrayList list1 = new ArrayList();
ArrayList list3 = new ArrayList();
ArrayList list6 = new ArrayList();
list1.add(10);
list1.add(20);
list1.add(32);
//list1.add(40);
ArrayList list2 = new ArrayList();
list2.add(11);
list2.add(21);
list2.add(31);
//list2.add(41);
ArrayList list4 = new ArrayList();
list4.add(10);
list4.add(20);
list4.add(30);
list4.add(40);
list4.add(50);
list4.add(60);
list4.add(32);
//list4.add(10);
ArrayList list5 = new ArrayList();
list5.add(11);
list5.add(21);
list5.add(31);
list5.add(41);
list5.add(51);
list5.add(61);
list5.add(31);
//list5.add(11);
int iSize = 0;
int iSize1 = 0;
String str = “”;
boolean blnFound;
if (list1.size()== list2.size()){
iSize = list1.size();
for (int i=0; i < iSize; i++){
list3.add(list1.get(i)+”.”+list2.get(i));
}
System.out.println(“New Array list3 : “+list3);
}else {
System.out.println(” Array1 Does not match Array2″);
}
if (list4.size()== list5.size()){
iSize1 = list4.size();
for (int i=0; i < iSize1; i++){
list6.add(list4.get(i)+”.”+list5.get(i));
}
System.out.println(“New Array list6 : “+list6);
}else {
System.out.println(” Array4 Does not match Array5″);
}
// Check if array6 contains array3
if(list1.size()== list2.size() && list4.size()== list5.size()){
for (int i=0; i< list3.size(); i++){
blnFound = list6.contains(list3.get(i));
if(blnFound){
str=”True”;
}
else{
str=”False”;
break;
}
}
System.out.println(“Result of Array3 Contains in Array6 is :”+str);
}
}
}
[/code]
Excuse me if I am incorrect.. but doesn’t the ‘contains’ method only return true if it finds the same binary object? So a statement such as array_list.contains(“Hi”) would never return true because the string “Hi” cannot exist in the ArrayList because we just created it.
dear sir i find this site really useful but these syntax are Java 1.5 but people want to study Java 1.6 So please change the code