nmu
nmu

Reputation: 1519

Is it possible to check if two arrays are not equal

I'm aware of the fact that I could just do a:

while(Arrays.equals(array1, array2))

and then just write the code needed in the else statement.

Is there any other way to check if they are not equal?

Upvotes: 3

Views: 15144

Answers (4)

Michael Yaworski
Michael Yaworski

Reputation: 13483

if ( !Arrays.equals(array1, array2) )
    // their contents are not equal

Upvotes: 0

Patrick Kostjens
Patrick Kostjens

Reputation: 5105

I don't think you want a while, but an if, since a while doesn't have an else-clause. You can use a negation operator (!) to check if the arrays are not equal like this:

if(!Arrays.equals(array1, array2))

Upvotes: 2

Karthik T
Karthik T

Reputation: 31972

How about

if (!Arrays.equals(array1, array2))

Or is that what you mean in your example?

Upvotes: 1

MrSimpleMind
MrSimpleMind

Reputation: 8647

Whats wrong with

if( !Arrays.equals(array1, array2) )

array1.equals(array2) is the same as array1 == array2, i.e. is it the same array. And it's not what most people expect.

Arrays.equals(array1, array2) compares the contents of the arrays.

Upvotes: 8

Related Questions