How to compare two arrays?
Previous | Home | Next |
This example shows how to compare two arrays.
We compare two type of Arrays The boolean arrays and int arrays.
Arrays.equal(boolean1[],boolean2[]) and Arrays.equal(int1[],int2][]) are used here two compare boolean and int arrays respectively
After creating Two arrays .call the Arrays.equal(array1[],array2[]) method which is boolean method and will return the result in true/false
package Example;
import java.util.Arrays;
public class CompareTwoArray {
/**
* @param args
*/
public static void main(String[] args) {
// to compare two boolean arrays
boolean[]Array1=new boolean[]{true,true,false,false};
boolean [] Array2 =new boolean []{true,false,true,true,};
boolean bln=Arrays.equals(Array1, Array2);
System.out.println("The two boolean arrays are equal:"+bln);
//to compare two int arrays
int[]Array3=new int[]{12,15,16,17,18};
int[]Array4=new int[]{02,5,6,7,8};
boolean bln1 =Arrays.equals(Array3, Array4);
System.out.println("The two int arrays are equal:"+bln1);
// TODO Auto-generated method stub
}
}
The two boolean arrays are equal:false
The two int arrays are equal:false
Previous | Home | Next |