Home > Java > javaTutorial > body text

Use java's Arrays.deepEquals() function to compare multidimensional arrays for equality.

王林
Release: 2023-07-25 08:12:29
Original
1330 people have browsed it

Use java's Arrays.deepEquals() function to compare whether multi-dimensional arrays are equal

In Java, if we need to compare whether two multi-dimensional arrays are equal, we can use deepEquals(() in the java.util.Arrays class )function. This function compares each element in a multidimensional array to determine whether two arrays are equal. In this article, we will introduce the method of using the Arrays.deepEquals() function to compare multi-dimensional arrays for equality and provide a code example.

First, let's start by creating and initializing two multidimensional arrays. In the following code example, we create two 2D arrays arr1 and arr2 and fill them with the same elements.

int[][] arr1 = { { 1, 2 }, { 3, 4 } };
int[][] arr2 = { { 1, 2 }, { 3, 4 } };
Copy after login

Now we can use the Arrays.deepEquals() function to compare these two multidimensional arrays. The code example is as follows:

boolean isEqual = Arrays.deepEquals(arr1, arr2);
if (isEqual) {
    System.out.println("arr1和arr2相等");
} else {
    System.out.println("arr1和arr2不相等");
}
Copy after login

Run this code, we will get the output result as "arr1 and arr2 are equal".

Here is a more complex example that demonstrates how to compare three-dimensional arrays for equality:

int[][][] arr1 = { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } };
int[][][] arr2 = { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } };

boolean isEqual = Arrays.deepEquals(arr1, arr2);
if(isEqual){
    System.out.println("arr1和arr2相等");
} else {
    System.out.println("arr1和arr2不相等");
}
Copy after login

Running this code, we will get the output as "arr1 and arr2 are equal".

It should be noted that the Arrays.deepEquals() function can only determine whether the elements in a multi-dimensional array are equal, but not whether the dimensions of the array are equal. For example, if the dimensions of two multidimensional arrays are different, false will be returned even if each element is equal.

In short, using Java's Arrays.deepEquals() function can conveniently compare the equality of multi-dimensional arrays. This function can recursively compare each element in a multi-dimensional array to determine whether the arrays are equal. Through the code examples provided in this article, you can use this function in your own projects to simplify the judgment of equality of multi-dimensional arrays.

The above is the detailed content of Use java's Arrays.deepEquals() function to compare multidimensional arrays for equality.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!