比较Java中的两个数组
在比较两个数组时,我们不能使用“==”运算符,因为它将比较两个数组指向的内存块的地址。如果我们想比较数组中的元素,我们需要找出其他方法而不是使用算术运算运算符。众所周知,数组数据结构具有以连续方式包含元素的属性,因此我们可以使用 Arrays 类本身的 size() 方法计算两个数组的大小,如果大小为发现两个数组相等。
插图:
Java
// Java Program to Illustrate
// Comparision of Arrays
// Using == Operators
// Importing required classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring integer arrays and
// initializing them with custom values
int arr1[] = { 1, 2, 3 };
int arr2[] = { 1, 2, 3 };
// Comparing arrays if equal or not
// using == operator
if (arr1 == arr2)
// Print statement
System.out.println("Same");
else
// Print statement
System.out.println("Not same");
}
}
Java
// Java Program to Check If Two Arrays Are Equal
// Using equals() method of Arrays class
// Importing required classes
import java.util.Arrays;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring integer arrays
int arr1[] = { 1, 2, 3 };
int arr2[] = { 1, 2, 3 };
// Checking if above two arrays are equal
// using equals() method
if (Arrays.equals(arr1, arr2))
// Print statement if arrays are equal
System.out.println("Same");
else
// Print statement if arrays are equal
System.out.println("Not same");
}
}
Java
// Java Program to Check If Two Arrays Are Equal
// Using equals() method of Arrays class
// Importing required classes
import java.util.Arrays;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing integer arrays
// having same values
// Array 1
int inarr1[] = { 1, 2, 3 };
// // Array 2
int inarr2[] = { 1, 2, 3 };
// // Array 1 contains only one element
Object[] arr1 = { inarr1 };
// Array 2 also contains only one element
Object[] arr2 = { inarr2 };
// Checking if arrays are equal or not
// using equals() method
if (Arrays.equals(arr1, arr2))
// Print statement if arrays are same
System.out.println("Same");
else
// Print statement if arrays are not same
System.out.println("Not same");
}
}
Java
// Java Program to Check If Two Arrays Are Equal
// Using deepEquals() method of Arrays class
// Importing required classes
import java.util.Arrays;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing integer arrays
// having same values
// Array 1
int inarr1[] = { 1, 2, 3 };
// Array 2
int inarr2[] = { 1, 2, 3 };
// Array 1 contains only one element
Object[] arr1 = { inarr1 };
// Array 2 also contains only one element
Object[] arr2 = { inarr2 };
// Checking if arrays are equal or not
// using deepEquals() method
if (Arrays.deepEquals(arr1, arr2))
// Print statement if arrays are same
System.out.println("Same");
else
// Print statement if arrays are not same
System.out.println("Not same");
}
}
Java
// Java Program to Check If Two Arrays Are Equal
// Using deepEquals() method of Arrays class
// Importing required classes
import java.util.Arrays;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing integer arrays
// having same values
// Array 1
int inarr1[] = { 1, 2, 3 };
// Array 2
int inarr2[] = { 1, 2, 3 };
// Array 1 contains only one element
Object[] arr1 = { inarr1 };
// Array 2 also contains only one element
Object[] arr2 = { inarr2 };
// outarr1 contains only one element
Object[] outarr1 = { arr1 };
// outarr2 also contains only one element
Object[] outarr2 = { arr2 };
// Checking if arrays are equal or not
// using deepEquals() method
if (Arrays.deepEquals(outarr1, outarr2))
// Print statement if arrays are same
System.out.println("Same");
else
// Print statement if arrays are not same
System.out.println("Not same");
}
}
Not same
输出说明:在Java中,数组是第一类对象。在上面的程序中,arr1 和 arr2 是对两个不同对象的两个引用。因此,当我们比较 arr1 和 arr2 时,会比较两个参考变量,因此我们得到的输出为“Not Same”。
如何比较数组内容?
一种简单的方法是运行一个循环并逐个比较元素。 Java提供了一个直接的方法Arrays.equals()来比较两个数组。实际上,Arrays 类中有一个 equals() 方法列表,用于不同的基本类型(int、char、..etc)和一个用于 Object 类型(它是Java中所有类的基础)。
例子:
Java
// Java Program to Check If Two Arrays Are Equal
// Using equals() method of Arrays class
// Importing required classes
import java.util.Arrays;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring integer arrays
int arr1[] = { 1, 2, 3 };
int arr2[] = { 1, 2, 3 };
// Checking if above two arrays are equal
// using equals() method
if (Arrays.equals(arr1, arr2))
// Print statement if arrays are equal
System.out.println("Same");
else
// Print statement if arrays are equal
System.out.println("Not same");
}
}
Same
输出说明:如上所示, Arrays.equals() 工作正常并比较数组内容。现在的问题是,如果数组内部包含数组或其他一些引用不同对象但具有相同值的引用,该怎么办?例如,参考下面的程序如下。
如何深度比较数组内容?
示例 1-A:
Java
// Java Program to Check If Two Arrays Are Equal
// Using equals() method of Arrays class
// Importing required classes
import java.util.Arrays;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing integer arrays
// having same values
// Array 1
int inarr1[] = { 1, 2, 3 };
// // Array 2
int inarr2[] = { 1, 2, 3 };
// // Array 1 contains only one element
Object[] arr1 = { inarr1 };
// Array 2 also contains only one element
Object[] arr2 = { inarr2 };
// Checking if arrays are equal or not
// using equals() method
if (Arrays.equals(arr1, arr2))
// Print statement if arrays are same
System.out.println("Same");
else
// Print statement if arrays are not same
System.out.println("Not same");
}
}
Not same
输出解释:所以Arrays.equals()无法进行深度比较。 Java为这个 Arrays.deepEquals() 提供了另一种方法来进行深度比较。
示例 1-B:
Java
// Java Program to Check If Two Arrays Are Equal
// Using deepEquals() method of Arrays class
// Importing required classes
import java.util.Arrays;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing integer arrays
// having same values
// Array 1
int inarr1[] = { 1, 2, 3 };
// Array 2
int inarr2[] = { 1, 2, 3 };
// Array 1 contains only one element
Object[] arr1 = { inarr1 };
// Array 2 also contains only one element
Object[] arr2 = { inarr2 };
// Checking if arrays are equal or not
// using deepEquals() method
if (Arrays.deepEquals(arr1, arr2))
// Print statement if arrays are same
System.out.println("Same");
else
// Print statement if arrays are not same
System.out.println("Not same");
}
}
Same
输出说明:让我们现在了解如何 Arrays 类的 deepEquals() 方法在内部工作。它使用它们可能具有的任何自定义 equals() 方法比较两个对象(如果它们实现了 Object.equals() 以外的 equals() 方法)。如果不是,则此方法将继续递归地逐个字段地比较对象。当遇到每个字段时,如果它存在,它将尝试使用派生的 equals(),否则,它将继续进一步递归。
This method works on a cyclic Object graph like this: A->B->C->A. It has cycle detection so ANY two objects can be compared, and it will never enter into an endless loop.
示例 1-C:
Java
// Java Program to Check If Two Arrays Are Equal
// Using deepEquals() method of Arrays class
// Importing required classes
import java.util.Arrays;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring and initializing integer arrays
// having same values
// Array 1
int inarr1[] = { 1, 2, 3 };
// Array 2
int inarr2[] = { 1, 2, 3 };
// Array 1 contains only one element
Object[] arr1 = { inarr1 };
// Array 2 also contains only one element
Object[] arr2 = { inarr2 };
// outarr1 contains only one element
Object[] outarr1 = { arr1 };
// outarr2 also contains only one element
Object[] outarr2 = { arr2 };
// Checking if arrays are equal or not
// using deepEquals() method
if (Arrays.deepEquals(outarr1, outarr2))
// Print statement if arrays are same
System.out.println("Same");
else
// Print statement if arrays are not same
System.out.println("Not same");
}
}
Same