Tuesday 25 March 2014

contains : Search element in the list

boolean contains(Object o)
    Returns true if this list contains the specified element.

import java.util.*;
class ListContains{
 public static void main(String args[]){
  List<String> myList = new Vector<String> ();
  
  /* Add Elements to the List */
  myList.add("Hi");
  myList.add("How are you");
  myList.add("abcd");
  myList.add("Vector");
  
  System.out.println("Elements in the list are ");
  System.out.println(myList);
  
  System.out.println("Is list contains the string abcd " + myList.contains("abcd"));
  System.out.println("Is list contains the string wxyz " + myList.contains("wxyz"));
 }
}

Output
Elements in the list are
[Hi, How are you, abcd, Vector]
Is list contains the string abcd true
Is list contains the string wxyz false


1. throws NullPointerException if the specified element is null and this list does not permit null elements

2. throws ClassCastException if the type of the specified element is incompatible with this list


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment