ArrayList

Search an element of Java ArrayList Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

Add Comment

  • [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

Sponsors

Facebook Fans

Online Shopping – Flipkart