Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java ArrayList contains

This Java ArrayList contains example shows how to check if particular object is in the ArrayList.
  1. import java.util.ArrayList;
  2.  
  3. //Java ArrayList contains
  4.  
  5. /*
  6.  * This Java ArrayList contains example shows how to check if particular
  7.  * object is in the ArrayList.
  8.  */
  9. public class JavaArrayListContains {
  10.     public static void main(String args[]){
  11.        
  12.         ArrayList<String> aListColors = new ArrayList<String>();
  13.         aListColors.add("Red");
  14.         aListColors.add("Green");
  15.         aListColors.add("Blue");
  16.        
  17.         /*
  18.          * To check if a particular element is present in ArrayList use,
  19.          * boolean contains(Object element)
  20.          * method of Java ArrayList class.
  21.          */
  22.        
  23.         //check if ArrayList contains element "Red"
  24.         boolean found = aListColors.contains("Red");
  25.        
  26.         if(found){
  27.             System.out.println("Red color found in ArrayList");
  28.         }else{
  29.             System.out.println("Red color not found in ArrayList");
  30.         }
  31.     }
  32. }
  33.  
  34. /*
  35. Output of above Java ArrayList contains example would be
  36. Red color found in ArrayList
  37. */
Your rating: None Average: 5 (2 votes)
Share this

Post new comment

11 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.