Can we compare 2 arrays in Java? - onlyxcodes

Wednesday, 3 July 2024

Can we compare 2 arrays in Java?

Hello, We will learn how to compare two arrays in Java in this lesson. In Java, it is possible to compare two arrays. In Java, the Java Arrays class has built-in methods for comparing two arrays. Depending on what kind of comparison you require, there are multiple ways to accomplish this:


This section teaches us how to compare two arrays using the loop method, Arrays.deepEquals(), and Arrays.equals() methods. In addition, with appropriate examples, we will discover how to carry out a thorough comparison between the two arrays.


compare two arrays in java

Table Content

1. Using Arrays.equals() Method

2. Using Arrays.deepEquals() Method

3. Using == operator

4. Using a loop


1. Using Arrays.equals() Method:

This function determines whether the contents of two arrays are equal by comparing them. If all matching pairs of elements in the two arrays are equal and both arrays have the same length, the function returns true.


import java.util.Arrays;

public class Test 
{
    public static void main(String[] args) 
	{
        int[] array1 = {1, 2, 3, 4, 5};
        int[] array2 = {1, 2, 3, 4, 5};
        
        boolean isEqual = Arrays.equals(array1, array2);
        System.out.println("Arrays.equals: " + isEqual); // Output: true
    }
}

Output:


Arrays.equals: true

2. Using Arrays.deepEquals() Method:

Nested arrays (arrays within arrays) can be compared using this method. It thoroughly compares the two arrays to determine their equality.


import java.util.Arrays;

public class Test
{
    public static void main(String[] args) 
	{
        int[][] array1 = {{1, 2, 3}, {4, 5, 6}};
        int[][] array2 = {{1, 2, 3}, {4, 5, 6}};
        
        boolean isEqual = Arrays.deepEquals(array1, array2);
        System.out.println("Arrays.deepEquals: " + isEqual); // Output: true
    }
}

Output:


Arrays.deepEquals: true

3. Using == operator:

This compares the two arrays' pointers rather than their contents. If both references relate to the same array instance, the function returns true.


public class Test
{
    public static void main(String[] args) 
	{
        int[] array1 = {1, 2, 3, 4, 5};
        int[] array2 = {1, 2, 3, 4, 5};
        int[] array3 = array1;
        
        System.out.println("array1 == array2: " + (array1 == array2)); // Output: false
        System.out.println("array1 == array3: " + (array1 == array3)); // Output: true
    }
}

Output:


array1 == array2: false
array1 == array3: true

4. Using a loop:

You can manually iterate through the arrays and compare each element.


public class Test
{
    public static void main(String[] args) 
	{
        int[] array1 = {1, 2, 3, 4, 5};
        int[] array2 = {1, 2, 3, 4, 5};
        
        boolean isEqual = true;
        if (array1.length != array2.length) 
		{
            isEqual = false;
        } 
		else 
		{
            for (int i = 0; i < array1.length; i++) {
                if (array1[i] != array2[i]) {
                    isEqual = false;
                    break;
                }
            }
        }
        
        System.out.println("Using loop: " + isEqual); // Output: true
    }
}

Output:


Using loop: true

Learn More:

Can you print a string array in Java

Can you Remove an Element from an Array in Java

How to Create a 2D Array Dynamically in Java

What is Array in Java with Example

What is the Long Array in Java with Example

What is Difference Between Array and ArrayList in Java

How to Add Value to an Array in Java

No comments:

Post a Comment

Post Bottom Ad