3:27 am - Friday May 26, 2017

Contains object program to check whether the cbject exist in the list using Contains method

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

Contains object program to check whether the cbject exist in the list using Contains() method

Example Program


import java.util.ArrayList;

public class ArrayListDemo {
	public static void main(String[] args) {

		// create an empty array list with an initial capacity
		ArrayList<Integer> al = new ArrayList<Integer>(8);

		// use add() method to add elements in the list
		al.add(20);
		al.add(25);
		al.add(10);
		al.add(15);

		// let us print all the elements available in list
		for (Integer number : al) {
			System.out.println("Number = " + number);
		}

		// list contains element 10
		boolean retval = al.contains(10);

		if (retval == true) {
			System.out.println("element 10 is contained in the list");
		} else {
			System.out.println("element 10 is not contained in the list");
		}

		// list does not contain element 30
		boolean retval2 = al.contains(30);

		if (retval2 == true) {
			System.out.println("element 30 is contained in the list");
		} else {
			System.out.println("element 30 is not contained in the list");
		}
	}
}

 

Output

 

Number = 20
Number = 25
Number = 10
Number = 15
element 10 is contained in the list
element 30 is not contained in the list

 

Description

 

The java.util.ArrayList.contains(Object) method returns true if this list contains the specified element.

 

Declaration

 

Following is the declaration for java.util.ArrayList.contains() method

public boolean contains(Object o)

 

Parameters

 

  • o — The element whose presence in this list is to be tested.

Return Value

 

This method returns true if this list contains the specified element.

 

Exception

 

  • NA

BACK

 

Filed in: Z-A All Java Codes

Comments are closed.