Compare two arrays

static boolean equals(boolean[] a, boolean[] a2)
Returns true if the two specified arrays of booleans are equal to one another.
static boolean equals(byte[] a, byte[] a2)
Returns true if the two specified arrays of bytes are equal to one another.
static boolean equals(char[] a, char[] a2)
Returns true if the two specified arrays of chars are equal to one another.
static boolean equals(double[] a, double[] a2)
Returns true if the two specified arrays of doubles are equal to one another.
static boolean equals(float[] a, float[] a2)
Returns true if the two specified arrays of floats are equal to one another.
static boolean equals(int[] a, int[] a2)
Returns true if the two specified arrays of ints are equal to one another.
static boolean equals(long[] a, long[] a2)
Returns true if the two specified arrays of longs are equal to one another.
static boolean equals(Object[] a, Object[] a2)
Returns true if the two specified arrays of Objects are equal to one another.
static boolean equals(short[] a, short[] a2)
Returns true if the two specified arrays of shorts are equal to one another.

Compare Two Java boolean Arrays with Arrays.equals() method.


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {

    boolean[] booleanArray1 = new boolean[] { true, false, true };
    boolean[] booleanArray2 = new boolean[] { true, false, true };
    

    System.out.println(Arrays.equals(booleanArray1, booleanArray2));
  }
}

The output:


true

Compare Two Java byte Arrays for equality using Arrays.equals method


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    byte[] byteArray1 = new byte[] { 1, 2, 3 };
    byte[] byteArray2 = new byte[] { 3, 2, 1 };

    System.out.println(Arrays.equals(byteArray1, byteArray2));
  }
}

The output:


false

Compare Two Java char Arrays for equality


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
 
    char[] charArray1 = new char[] { 'j', 'a', 'v', 'a' };
    char[] charArray2 = new char[] { 'j', 'a', 'v', 'a' };
    
    System.out.println(Arrays.equals(charArray1, charArray2));
  }
}

The output:


true

Compare Two Java double Arrays for equality


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
 
    double[] doubleArray1 = new double[] { 1.1, 2.2, 3.3 };
    double[] doubleArray2 = new double[] { 1.1, 2.2, 3.3 };
    
    System.out.println(Arrays.equals(doubleArray1, doubleArray2));
  }
}

The output:


true

Compare Two Java float Arrays


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
 
    float[] floatArray1 = new float[] { 1.2f, 2.3f, 3.3f };
    float[] floatArray2 = new float[] { 1.2f, 2.3f, 3.3f };
    System.out.println(Arrays.equals(floatArray1, floatArray2));
  }
}

The output:


true

Compare Two Java int Arrays for equality using Arrays.equals method.


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
 
    int[] intArray1 = new int[] { 1, 2, 3 };
    int[] intArray2 = new int[] { 1, 2, 3 };
    
    System.out.println(Arrays.equals(intArray1, intArray2));
  }
}

The output:


true

Compare Two Java long Arrays for equality using Arrays.equals method.


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
 
    long[] longArray1 = new long[] { 999999999, 999999999, 999999999 };
    long[] longArray2 = new long[] { 999999999, 999999999, 999999999 };
 
    System.out.println(Arrays.equals(longArray1, longArray2));
  }
}

The output:


true

Compare Two Java short Arrays for equality using Arrays.equals method.


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    short[] shortArray1 = new short[] { 107, 93, 58 };
    short[] shortArray2 = new short[] { 107, 93, 58 };
    
    System.out.println(Arrays.equals(shortArray1, shortArray2));
  }
}

The output:


true
Home 
  Java Book 
    Collection  

Arrays:
  1. Arrays
  2. Convert array to list
  3. Binary search an Array
  4. Deep to string
  5. Compare two arrays
  6. Fill value to array
  7. Sort an array
  8. Convert array to string