比较两个浮点数组的Java程序
数组是存储在连续内存位置的项目的集合。思路是将多个相同类型的项存储在一起。基值是索引0,两个索引之间的差异是偏移量。
在这个程序中,我们需要比较两个具有浮点类型元素的数组。
Two arrays are equal if they contain the same elements in the same order. Two array references are considered equal if both are null.
例子:
float[] floatV1 = new float[] { 3.1f, 7.5f, 8.3f };
float[] floatV2 = new float[] { 8.3f, 8.8f, 9.2f };
float[] floatV3 = new float[] { 3.1f, 7.5f, 8.3f };
float[] floatV4 = new float[] { 3.2f, 5.5f, 5.3f };
if we compare the two arrays using Arrays.equals() method, it will return true/false.
Arrays.equals(floatV1, floatV2) will give us false
Arrays.equals(floatV3, floatV4) will give us false
Arrays.equals(floatV1, floatV3) will give us true
有两种方法可以比较两个浮点数组:
- 通过简单地遍历整个数组并一一比较每个元素。
- 使用 Arrays.equals(arr1,arr2) 方法
方法一:通过简单地遍历整个数组并一一比较每个元素。
Java
// Java program to compare two float arrays
// using simple iteration
import java.util.Arrays;
public class GFG {
public static void main(String args[])
{
// declaring values in float arrays
float[] floatV1 = new float[] { 3.1f, 7.5f, 8.3f };
float[] floatV2 = new float[] { 8.9f, 8.8f, 9.2f };
float[] floatV3 = new float[] { 3.1f, 7.5f, 8.3f };
System.out.println(compareArrays(floatV2, floatV3));
System.out.println(compareArrays(floatV1, floatV3));
System.out.println(compareArrays(floatV1, floatV2));
}
public static boolean compareArrays(float arr1[],float arr2[])
{
// if the length of the two arrays are not
// same then the arrays cannot be equal
if (arr1.length != arr2.length)
return false;
for (int i = 0; i < arr1.length; i++) {
// if the two ith elements of the two arrays
// are not same then simply return false
// and do not check further elements
if (arr1[i] != arr2[i])
return false;
}
// else if we come out of the for loop
// successfully without returning false ,
// then that means all the elements were equal
return true;
}
}
Java
// Java program to compare two float arrays
// using Arrays.equals() method
import java.util.Arrays;
public class GFG {
public static void main(String args[])
{
// declaring values in float arrays
float[] floatV1 = new float[] { 3.1f, 7.5f, 8.3f };
float[] floatV2 = new float[] { 8.9f, 8.8f, 9.2f };
float[] floatV3 = new float[] { 3.1f, 7.5f, 8.3f };
float[] floatV4 = new float[] { 3.4f, 5.5f, 5.3f };
// printing and comparing the arrays
// as it will return boolean value
// i.e. either true or false
System.out.println(Arrays.equals(floatV1, floatV2));
System.out.println(Arrays.equals(floatV2, floatV3));
System.out.println(Arrays.equals(floatV3, floatV4));
System.out.println(Arrays.equals(floatV1, floatV3));
System.out.println(Arrays.equals(floatV1, floatV4));
}
}
输出
false
true
false
方法 2:使用Arrays.equals()方法
句法:
public static boolean equals(int[] a, int[] a2)
参数:
- a – 一个要测试相等性的数组
- a2 – 要测试相等性的另一个数组
返回:如果两个数组相等则为真
Java
// Java program to compare two float arrays
// using Arrays.equals() method
import java.util.Arrays;
public class GFG {
public static void main(String args[])
{
// declaring values in float arrays
float[] floatV1 = new float[] { 3.1f, 7.5f, 8.3f };
float[] floatV2 = new float[] { 8.9f, 8.8f, 9.2f };
float[] floatV3 = new float[] { 3.1f, 7.5f, 8.3f };
float[] floatV4 = new float[] { 3.4f, 5.5f, 5.3f };
// printing and comparing the arrays
// as it will return boolean value
// i.e. either true or false
System.out.println(Arrays.equals(floatV1, floatV2));
System.out.println(Arrays.equals(floatV2, floatV3));
System.out.println(Arrays.equals(floatV3, floatV4));
System.out.println(Arrays.equals(floatV1, floatV3));
System.out.println(Arrays.equals(floatV1, floatV4));
}
}
输出
false
false
false
true
false