EN
Java - compare arrays
5
points
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