Home Tutorial Java Collections Arraylist Java arraylist contains

 
 

Share on Google+Share on Google+
Java arraylist contains
Posted on: October 23, 2009 at 12:00 AM
Advertisement
This section demonstrates the use of contains() method of the ArrayList

  • contains() method is used to check whether the element is present or not.
  • It returns boolean value i.e. true or false.


Example of Java Arraylist Contains
import java.util.ArrayList;
import java.util.List;

public class list_contain {
    public static void main(String[] args) {
        List list=new ArrayList();
        String names[]={"mac","john","alexender","rock","alex"};
        for (int i = 0; i < names.length; i++) {
        list.add(names[i]);
        }
     if(list.contains("rock")) {
        System.out.println("rock is present in the list ");
        }
     else {
        System.out.println("rock   is not present in the list ");
        }
    }
}
output
rock is present in the list
Advertisement

Related Tags for Java arraylist contains:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 23, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com