Comparing two arrays

If you want to compare if two arrays point to the same memory, you can simply compare its references:

import java.util.*;
import java.io.*;
 
public class Main
{
   public static void main(String []args) {
      int arr[] = { 10, 8, 6, 4, 2, 11, 3, 5, 7, 9 };
 
      int arr2[] = arr;
 
      if (arr == arr2) {
         System.out.println("Arrays are equal"); 
      } 
      else {
         System.out.println("Arrays are not equal"); 
      }
   }
}

If you want to compare each and one of the values of the array’s elements are equal, you can use the handy method equals in the Arrays class. Here’s an example:

import java.util.*;
import java.io.*;
 
public class Main
{
   public static void main(String []args) {
      int arr[] = { 10, 8, 6, 4, 2, 11, 3, 5, 7, 9 }; 
      int arr2[] = { 10, 3, 6, 4, 2, 11, 3, 5, 7, 9 };
 
      if (Arrays.equals(arr, arr2)) {
         System.out.println("Arrays are equal");
      }
      else {
         System.out.println("Arrays are not equal");
      }
   }
}