Java Program to compare Two Java float Arrays

Java 8Object Oriented ProgrammingProgramming

To compare Java float arrays, use the Arrays.equals() method. The return value is a boolean. Let’s say we have the following float arrays −

float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f };
float[] floatVal2 = new float[] { 8.3f, 8.8f, 9.2f };
float[] floatVal3 = new float[] { 6.2f, 6.9f, 9.9f };
float[] floatVal4 = new float[] { 3.2f, 5.5f, 5.3f };

To compare them, the Arrays.equals() method is to be used −

Arrays.equals(floatVal1, floatVal2);
Arrays.equals(floatVal2, floatVal3);
Arrays.equals(floatVal3, floatVal4);

The following is the complete example wherein we compare all the arrays −

Example

 Live Demo

import java.util.Arrays;
public class Demo {
   public static void main(String args[]) {
      float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f };
      float[] floatVal2 = new float[] { 8.3f, 8.8f, 9.2f };
      float[] floatVal3 = new float[] { 6.2f, 6.9f, 9.9f };
      float[] floatVal4 = new float[] { 3.2f, 5.5f, 5.3f };
      System.out.println(Arrays.equals(floatVal1, floatVal2));
      System.out.println(Arrays.equals(floatVal2, floatVal3));
      System.out.println(Arrays.equals(floatVal3, floatVal4));
      System.out.println(Arrays.equals(floatVal1, floatVal3));
      System.out.println(Arrays.equals(floatVal1, floatVal4));
   }
}

Output

false
false
false
false
true
raja
Published on 10-Dec-2018 12:09:22
Advertisements