Compare int Array and int Array Copy for equality - Java java.util

Java examples for java.util:Arrays

Description

Compare int Array and int Array Copy for equality

Demo Code

import java.util.Arrays;

public class Main 
{
   public static void main(String[] args)
   {//from   www .j  a v  a  2  s.c om
     // copy array intArray into array intArrayCopy
     int[] intArray = {1, 2, 3, 4, 5, 6};
     int[] intArrayCopy = new int[intArray.length];
     System.arraycopy(intArray, 0, intArrayCopy, 0, intArray.length);
     displayArray(intArray, "intArray");
     displayArray(intArrayCopy, "intArrayCopy");
     
     // compare intArray and intArrayCopy for equality
     boolean b = Arrays.equals(intArray, intArrayCopy);
     System.out.printf("%n%nintArray %s intArrayCopy%n",
        (b ? "==" : "!="));
   } 

   // output values in each array
   public static void displayArray(int[] array, String description)
   {     
      System.out.printf("%n%s: ", description);

      for (int value : array)
         System.out.printf("%d ", value);
   } 
}

Result


Related Tutorials