Java Program To Check The Equality Of Two Arrays
=========================================================================
1) Approach -1 (By Using Comparision and Equality)
public class CheckEqualityArray2 {
public static void main(String[] args) {
int a[] = { 1, 2, 3, 4, 5 };
int b[] = { 1, 2, 3, 4, 5 };
boolean status = true;
if (a.length == b.length) {
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
status = false;
}
}
} else {
status = false;
}
if (status == true) {
System.out.println("Arrays Are Equal");
} else {
System.out.println("Arrays are not Equal");
}
}
}
2) Approach -2 (By Using Arrays class In Util Package)
import java.util.Arrays;
public class CheckEqualityArray {
public static void main(String[] args) {
int a[] = { 1, 2, 3, 4, 5 };
int b[] = { 1, 2, 3, 4, 5 };
boolean status=Arrays.equals(a, b);
if(status == false)
{
System.out.println("Arrays are not equal");
}
else{
System.out.println("Arrays are equal");
}
}
}
Comments
Post a Comment