Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java ArrayList contains() Method

Java ArrayList contains() Method

Java ArrayList Java ArrayList

The contains() method is used to determine whether an element is in the dynamic array.

The syntax of the contains() method is:


arraylist.contains(Object obj)

**Note:** arraylist is an object of the ArrayList class.

Parameter Description:

Return Value

If the specified element exists in the dynamic array, returns true.

If the specified element does not exist in the dynamic array, returns false.

Example

A dynamic array of type String using the contains() method:

Example

import

java.util.ArrayList

;

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create an array

        ArrayList

<

String

sites

=

new

ArrayList

<>

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

       

System

.

out

.

println

(

"Website list: "

sites

)

;

       

// Check whether Runoob is in this array

       

System

.

out

.

print

(

"Does Runoob exist in arraylist: "

)

;

       

System

.

out

.

println

(

sites.

contains

(

"Runoob"

)

)

;

       

// Check whether Weibo is in this array

       

System

.

out

.

print

(

"Does Weibo exist in arraylist: "

)

;

       

System

.

out

.

println

(

sites.

contains

(

"Weibo"

)

)

;

   

}

}

The output of the above program is:


Runoob 是否存在于 arraylist: true
Weibo 是否存在于 arraylist: false

In the example above, we create a dynamic array named sites. Note the expression:

sites.contains("Runoob")
sites.contains("Weibo")

In the code above, the contains() method checks whether Runoob exists in the dynamic array. Since Runoob exists, the method returns true. However, Weibo does not exist in the list, so the method returns false.

**Note:** The contains() method internally actually uses the equals() method to find elements. If the specified element matches an element in the array, the method returns true.

A dynamic array of type Integer using the contains() method:

Example

import

java.util.ArrayList

;

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create an array

        ArrayList

<

Integer

numbers

=

new

ArrayList

<>

(

)

;

       

// Insert elements into the array

        numbers.

add

(

2

)

;

        numbers.

add

(

3

)

;

        numbers.

add

(

5

)

;

       

System

.

out

.

println

(

"Number ArrayList: "

numbers

)

;

       

// Check whether 3 is in this array

       

System

.

out

.

print

(

"Is 3 in arraylist: "

)

;

       

System

.

out

.

println

(

numbers.

contains

(

3

)

)

;

       

// Check whether 1 is in this array

       

System

.

out

.

print

(

"Is 1 in arraylist: "

)

;

       

System

.

out

.

println

(

numbers.

contains

(

1

)

)

;

   

}

}

The output of the above program is:


Number ArrayList: [2, 3, 5]
3 是否在 arraylist: true
1 是否在 arraylist: false

In the code above, the contains() method checks whether 3 exists in the dynamic array. Since 3 exists, the method returns true. However, 1 does not exist in the list, so the method returns false.

Java ArrayList Java ArrayList