Languages
[Edit]
EN

Java - compare arrays

5 points
Created by:
Tomeriko
263

In this short article, we would like to show how to compare 2 arrays in Java.

Note: as arrays comparision we understand comparision on array items.

Quick solution:

// import java.util.Arrays;

String[] a = {"1", "2", "3"};
String[] b = {"1", "2", "3"};

System.out.println(Arrays.equals(a, b));  // true

Ā 

Items comparion example

In this section, we can see items comparions example.

import java.util.Arrays;

public class HelloWorld{

     public static void main(String []args){

        String[] a = {"1", "2", "3"};
        String[] b = {"1", "2", "3"};

        System.out.println(Arrays.equals(a, b));  // true
     }
}

Output:

true

ReferencesĀ comparion example

The example presented in this section is useful when we want to check if 2 variables indicateĀ same array.

import java.util.Arrays;

public class HelloWorld{

    public static void main(String []args){

        String[] a = {"1", "2", "3"};
        String[] b = {"1", "2", "3"};

        System.out.println(a == b);  // false
        System.out.println(a == a);  // true
        System.out.println(b == b);  // true
    }
}

Output:

false
true
true

Ā 

Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join