Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- import java.util.ArrayList;
- //Java ArrayList contains
- /*
- * This Java ArrayList contains example shows how to check if particular
- * object is in the ArrayList.
- */
- public class JavaArrayListContains {
- public static void main(String args[]){
- ArrayList<String> aListColors = new ArrayList<String>();
- aListColors.add("Red");
- aListColors.add("Green");
- aListColors.add("Blue");
- /*
- * To check if a particular element is present in ArrayList use,
- * boolean contains(Object element)
- * method of Java ArrayList class.
- */
- //check if ArrayList contains element "Red"
- boolean found = aListColors.contains("Red");
- if(found){
- System.out.println("Red color found in ArrayList");
- }else{
- System.out.println("Red color not found in ArrayList");
- }
- }
- }
- /*
- Output of above Java ArrayList contains example would be
- Red color found in ArrayList
- */

Post new comment