Java 程序比较两个 Java 浮点数组
要比较 Java 浮点数组,请使用 Arrays.equals() 方法。返回值为布尔值。假设我们有以下浮点数组 -
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
广告