Java DoubleBuffer equals()方法及示例

来源:这里教程网 时间:2026-02-17 20:48:58 作者:

Java DoubleBuffer equals()方法及示例

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

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

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

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

双重缓冲区不等于任何其他类型的对象。

语法

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 DoubleBuffer 1        int capacity1 = 10;          // Declaring the capacity of the  DoubleBuffer 2        int capacity2 = 10;          // Creating the DoubleBuffer        try {              // creating object of Doublebuffer 1            // and allocating size capacity            DoubleBuffer db1 = DoubleBuffer.allocate(capacity1);              // creating object of Doublebuffer 2            // and allocating size capacity            DoubleBuffer db2 = DoubleBuffer.allocate(capacity2);              // putting the value in Doublebuffer 1            db1.put(8.56F);            db1.put(2, 9.61F);            db1.rewind();              // putting the value in Doublebuffer 2            db2.put(8.56F);            db2.put(2, 9.61F);            db2.rewind();              // print the DoubleBuffer 1            System.out.println(" DoubleBuffer 1:  "                               + Arrays.toString(db1.array()));              // print the DoubleBuffer 2            System.out.println(" DoubleBuffer 2:  "                               + Arrays.toString(db2.array()));              // checking the equality of both DoubleBuffer            boolean dbb = db1.equals(db2);              // checking if else condition            if (dbb)                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");        }    }}

输出:

DoubleBuffer 1:  [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] DoubleBuffer 2:  [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]both are equal

例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 DoubleBuffer 1        int capacity1 = 10;          // Declaring the capacity of the  DoubleBuffer 2        int capacity2 = 5;          // Creating the DoubleBuffer        try {              // creating object of Doublebuffer 1            // and allocating size capacity            DoubleBuffer db1 = DoubleBuffer.allocate(capacity1);              // creating object of Doublebuffer 2            // and allocating size capacity            DoubleBuffer db2 = DoubleBuffer.allocate(capacity2);              // putting the value in Doublebuffer 1            db1.put(8.56F);            db1.put(2, 9.61F);            db1.rewind();              // putting the value in Doublebuffer 2            db2.put(8.56F);            db2.put(2, 9.61F);            db2.rewind();              // print the DoubleBuffer 1            System.out.println(" DoubleBuffer 1:  "                               + Arrays.toString(db1.array()));              // print the DoubleBuffer 2            System.out.println(" DoubleBuffer 2:  "                               + Arrays.toString(db2.array()));              // checking the equality of both DoubleBuffer            boolean dbb = db1.equals(db2);              // checking if else condition            if (dbb)                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");        }    }}

输出:

DoubleBuffer 1:  [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] DoubleBuffer 2:  [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0]both are not equal

相关推荐