Enter your E-mail Address below for Free E-mail Alerts right Into your Inbox: -

Sunday

How to compare two Arrays in java

How to compare two Arrays in java

 public final class Test {


void comparaeAll()
{
int a1[] = {1, 2, 3};
       int a2[] = {1, 2, 3};
       if (a1 == a2) // Same as arr1.equals(arr2)
           System.out.println("equal");
       else
           System.out.println("Not equal");
}


}


what will be the output for the above program.

Output:- Not equal

public final class Test {


void comparaeAll()
{
int a1[] = {1, 2, 3};
       int a2[] = {1, 2, 3};
       if (Arrays.equals(a1, a2)) // Same as a1.equals(a2)
           System.out.println("equal");
       else
           System.out.println("Not equal");
}

}


For this what will be the output

Output:- equal

Use Arrays.equals() for comparing two arrays.


No comments:

Post a Comment