This documentation is archived and is not being maintained.

ArrayList.contains Method

Visual Studio 2005

Determines whether an element exists in a ArrayList object.

Package: java.util

Assembly: vjslib (in vjslib.dll)

public boolean contains(
    java.lang.Object e);



The element for which the ArrayList object is to be scanned to determine whether it exists in the collection.

Returns true if the element exists in the ArrayList object; false otherwise.

The following example searches an ArrayList to determine whether it contains various prime numbers.

// arraylist_contains.jsl

import java.util.*;

public class Program
    public static void main(String[] args)
        // Fill an array with the prime numbers between
        // 1 and 11.
        ArrayList primeNumbers = new ArrayList();
        primeNumbers.add(new Integer(1));
        primeNumbers.add(new Integer(3));
        primeNumbers.add(new Integer(5));
        primeNumbers.add(new Integer(7));
        primeNumbers.add(new Integer(11));

        // Determines whether all the numbers between 1 and 11
        // are prime or not prime.
        for (int i = 1; i <= 11; i++)
            if (primeNumbers.contains(new Integer(i)))
                System.out.println(i + " is a prime number.");
                System.out.println(i + " is NOT a prime number.");

1 is a prime number.
2 is NOT a prime number.
3 is a prime number.
4 is NOT a prime number.
5 is a prime number.
6 is NOT a prime number.
7 is a prime number.
8 is NOT a prime number.
9 is NOT a prime number.
10 is NOT a prime number.
11 is a prime number.