在 Java 中检查两个浮点数组是否相等
要检查两个浮点数组是否相等,请使用 Arrays.equals() 方法。
在我们的示例中,我们有以下两个浮点数组。
float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f };
float[] floatVal2 = new float[] { 3.2f, 5.5f, 5.3f };现在让我们比较它们是否相等。
if (Arrays.equals(floatVal1, floatVal2)) {
System.out.println("Both are equal!");
}以下是一个示例,在其中我们比较数组,同时使用 == 来检查其他条件。
示例
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[] { 3.2f, 5.5f, 5.3f };
if (floatVal1 == null) {
System.out.println("First array is null!");
}
if (floatVal2 == null) {
System.out.println("Second array is null!");
}
if (floatVal1.length != floatVal2.length) {
System.out.println("Both does not have equal number of elements!");
}
if (Arrays.equals(floatVal1, floatVal2)) {
System.out.println("Both are equal!");
}
}
}输出
Both are equal!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言
C++
C#
MongoDB
MySQL
Javascript
PHP