Java 是否可以检查两个数组是否不相等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18310198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:22:29  来源:igfitidea点击:

Is it possible to check if two arrays are not equal

javaarrays

提问by nmu

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 elsestatement.

然后只需编写else语句中所需的代码。

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

有没有其他方法可以检查它们是否不相等?

采纳答案by MrSimpleMind

Whats wrong with

怎么了

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

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.

array1.equals(array2)与 相同array1 == array2,即它是同一个数组。这不是大多数人所期望的。

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

Arrays.equals(array1, array2)比较数组的内容。

回答by Karthik T

How about

怎么样

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

Or is that what you mean in your example?

或者这就是你在你的例子中的意思?

回答by Patrick Kostjens

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

我不认为你想要 a while,而是一个 if,因为 awhile没有 -else子句。您可以使用否定运算符 ( !) 来检查数组是否不相等,如下所示:

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

回答by Michael Yaworski

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