Java程序比较两个Java浮点数组
要比较Java浮点数组,请使用Arrays.equals()方法。返回值是一个布尔值。假设我们有以下float数组-
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 };
为了比较它们,将使用Arrays.equals()方法-
Arrays.equals(floatVal1, floatVal2); Arrays.equals(floatVal2, floatVal3); Arrays.equals(floatVal3, floatVal4);
以下是完整的示例,其中我们比较了所有数组-
示例
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)); } }
输出结果
false false false false true