Java FloatBuffer equals()方法及示例

Java FloatBuffer equals()方法及示例

java.nio.FloatBuffer 类的 equals() 方法用于检查给定的缓冲区是否与另一个对象相等。

两个Float缓冲区是相等的,当且仅当。

  • 它们具有相同的元素类型。
  • 它们有相同数量的剩余元素,并且
  • 两个剩余元素的序列,独立于其起始位置考虑,是点状的 。

这个方法认为两个Float元素a和b是相等的,如果(a == b)||(Float.isNaN(a) && Float.isNaN(b))。值-0.0和+0.0被认为是相等的,与Float.equals(Object)不同。

Float缓冲器不等于任何其他类型的对象。

语法

public boolean equals(Object ob)

参数: 该方法以 ob (这个缓冲区要被比较的对象)为参数。

返回值: 当且仅当这个缓冲区等于给定的对象时,该方法返回真。

下面是说明 equals() 方法的例子。

例子 1 :

// Java program to demonstrate
// equals() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the  FloatBuffer 2
        int capacity2 = 10;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer 1
            // and allocating size capacity
            FloatBuffer fb1 = FloatBuffer.allocate(capacity1);
  
            // creating object of floatbuffer 2
            // and allocating size capacity
            FloatBuffer fb2 = FloatBuffer.allocate(capacity2);
  
            // putting the value in floatbuffer 1
            fb1.put(8.56F);
            fb1.put(2, 9.61F);
            fb1.rewind();
  
            // putting the value in floatbuffer 2
            fb2.put(8.56F);
            fb2.put(2, 9.61F);
            fb2.rewind();
  
            // print the FloatBuffer 1
            System.out.println(" FloatBuffer 1:  "
                               + Arrays.toString(fb1.array()));
  
            // print the FloatBuffer 2
            System.out.println(" FloatBuffer 2:  "
                               + Arrays.toString(fb2.array()));
  
            // checking the equality of both FloatBuffer
            boolean fbb = fb1.equals(fb2);
  
            // checking if else condition
            if (fbb)
                System.out.println("both are equal");
            else
                System.out.println("both are not equal");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}

输出。

FloatBuffer 1:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
FloatBuffer 2:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
both are equal

例子 2 :

// Java program to demonstrate
// equals() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the  FloatBuffer 2
        int capacity2 = 5;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer 1
            // and allocating size capacity
            FloatBuffer fb1 = FloatBuffer.allocate(capacity1);
  
            // creating object of floatbuffer 2
            // and allocating size capacity
            FloatBuffer fb2 = FloatBuffer.allocate(capacity2);
  
            // putting the value in floatbuffer 1
            fb1.put(8.56F);
            fb1.put(2, 9.61F);
            fb1.rewind();
  
            // putting the value in floatbuffer 2
            fb2.put(8.56F);
            fb2.put(2, 9.61F);
            fb2.rewind();
  
            // print the FloatBuffer 1
            System.out.println(" FloatBuffer 1:  "
                               + Arrays.toString(fb1.array()));
  
            // print the FloatBuffer 2
            System.out.println(" FloatBuffer 2:  "
                               + Arrays.toString(fb2.array()));
  
            // checking the equality of both FloatBuffer
            boolean fbb = fb1.equals(fb2);
  
            // checking if else condition
            if (fbb)
                System.out.println("both are equal");
            else
                System.out.println("both are not equal");
        }
        catch (IllegalArgumentException e) {
  
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}

输出。

FloatBuffer 1:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
FloatBuffer 2:  [8.56, 0.0, 9.61, 0.0, 0.0]
both are not equal

例子 3 :

// Java program to demonstrate
// equals() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the FloatBuffer 1
        int capacity1 = 10;
  
        // Declaring the capacity of the  FloatBuffer 2
        int capacity2 = 10;
  
        // Creating the FloatBuffer
        try {
  
            // creating object of floatbuffer 1
            // and allocating size capacity
            FloatBuffer fb1 = FloatBuffer.allocate(capacity1);
  
            // creating object of floatbuffer 2
            // and allocating size capacity
            FloatBuffer fb2 = FloatBuffer.allocate(capacity2);
  
            // putting the value in floatbuffer 1
            fb1.put(8.56F);
            fb1.put(2, 9.61F);
            fb1.rewind();
  
            // putting the value in floatbuffer 2
            fb2.put(8.56F);
            fb2.put(2, 9.61F);
            fb2.put(3, 7.861F);
            fb2.put(4, 4.31F);
            fb2.rewind();
  
            // print the FloatBuffer 1
            System.out.println(" FloatBuffer 1:  "
                               + Arrays.toString(fb1.array()));
  
            // print the FloatBuffer 2
            System.out.println(" FloatBuffer 2:  "
                               + Arrays.toString(fb2.array()));
  
            // checking the equality of both FloatBuffer
            boolean fbb = fb1.equals(fb2);
  
            // checking if else condition
            if (fbb)
                System.out.println("both are equal");
            else
                System.out.println("both are not equal");
        }
        catch (IllegalArgumentException e) {
  
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
  
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}

输出。

FloatBuffer 1:  [8.56, 0.0, 9.61, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
FloatBuffer 2:  [8.56, 0.0, 9.61, 7.861, 4.31, 0.0, 0.0, 0.0, 0.0, 0.0]
both are not equal

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程