- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What does the method contains(obj o) do in java?
The contains(Object) method of the java.util.ArrayList class returns true if this list contains the specified element.
Example
import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList<Integer> arrlist = new ArrayList<Integer<(8); arrlist.add(20); arrlist.add(25); arrlist.add(10); arrlist.add(15); for (Integer number : arrlist) { System.out.println("Number = " + number); } boolean retval = arrlist.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"); } boolean retval2 = arrlist.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
Advertisements