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

Java Example – Checking Whether Arrays Are Equal

Java Example – Checking Whether Arrays Are Equal

Java Examples Java Examples

The following example demonstrates how to use the equals() method to check whether two arrays are equal:

Main.java File

import

java

.

util

.

Arrays

;

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

throws

Exception

{

int

[

]

ary

=

{

1

,

2

,

3

,

4

,

5

,

6

}

;

int

[

]

ary1

=

{

1

,

2

,

3

,

4

,

5

,

6

}

;

int

[

]

ary2

=

{

1

,

2

,

3

,

4

}

;

System

.

out

.

println

(

"

Is array ary equal to array ary1?:

"

Arrays

.

equals

(

ary

,

ary1

)

)

;

System

.

out

.

println

(

"

Is array ary equal to array ary2?:

"

Arrays

.

equals

(

ary

,

ary2

)

)

;

}

}

The output of the above code is:



数组 ary 是否与数组 ary1相等? :true

数组 ary 是否与数组 ary2相等? :false

Java Examples Java Examples