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

Java Example – Finding a Specified Element in an Array

Java Example – Finding a Specified Element in an Array

Java Examples Java Examples

The following example demonstrates how to use the contains() method to find a specified element in an array:

Main.java File

import

java

.

util

.

ArrayList

;

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

{

ArrayList

<

String

objArray

=

new

ArrayList

<

String

(

)

;

ArrayList

<

String

objArray2

=

new

ArrayList

<

String

(

)

;

objArray2

.

add

(

0

,

"

common1

"

)

;

objArray2

.

add

(

1

,

"

common2

"

)

;

objArray2

.

add

(

2

,

"

notcommon

"

)

;

objArray2

.

add

(

3

,

"

notcommon1

"

)

;

objArray

.

add

(

0

,

"

common1

"

)

;

objArray

.

add

(

1

,

"

common2

"

)

;

System

.

out

.

println

(

"

Elements of objArray:

"

objArray

)

;

System

.

out

.

println

(

"

Elements of objArray2:

"

objArray2

)

;

System

.

out

.

println

(

"

Does objArray contain the string common2?:

"

objArray

.

contains

(

"

common2

"

)

)

;

System

.

out

.

println

(

"

Does objArray2 contain the array objArray?:

"

objArray2

.

contains

(

objArray

)

)

;

}

}

The output of the above code is:



objArray 的数组元素:[common1, common2]

objArray2 的数组元素:[common1, common2, notcommon, notcommon1]

objArray 是否包含字符串common2? : true

objArray2 是否包含数组 objArray? :false

Java Examples Java Examples