Java中的IntBuffer equals()方法

这个 等于 方法 JAVA尼奥。IntBuffer 类用于检查给定的缓冲区是否等于另一个对象。

null

两个int缓冲区相等当且仅当,

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

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

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

语法:

public boolean equals(Object ob)

参数: 这种方法需要 ob ,将此缓冲区与之进行比较的对象,作为参数。

返回值: 此方法返回 符合事实的 当且仅当该缓冲区等于给定对象。

下面是一些例子来说明 等于 方法:

例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 IntBuffer 1
int capacity1 = 10 ;
// Declaring the capacity of the  IntBuffer 2
int capacity2 = 10 ;
// Creating the IntBuffer
try {
// creating object of Intbuffer 1
// and allocating size capacity
IntBuffer ib1 = IntBuffer.allocate(capacity1);
// creating object of Intbuffer 2
// and allocating size capacity
IntBuffer ib2 = IntBuffer.allocate(capacity2);
// putting the value in Intbuffer 1
ib1.put( 8 );
ib1.put( 2 , 9 );
ib1.rewind();
// putting the value in Intbuffer 2
ib2.put( 8 );
ib2.put( 2 , 9 );
ib2.rewind();
// print the IntBuffer 1
System.out.println( " IntBuffer 1:  "
+ Arrays.toString(ib1.array()));
// print the IntBuffer 2
System.out.println( " IntBuffer 2:  "
+ Arrays.toString(ib2.array()));
// checking the equality of both IntBuffer
boolean ibb = ib1.equals(ib2);
// checking if else condition
if (ibb)
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" );
}
}
}


输出:

IntBuffer 1:  [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
 IntBuffer 2:  [8, 0, 9, 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 IntBuffer 1
int capacity1 = 10 ;
// Declaring the capacity of the  IntBuffer 2
int capacity2 = 5 ;
// Creating the IntBuffer
try {
// creating object of Intbuffer 1
// and allocating size capacity
IntBuffer ib1 = IntBuffer.allocate(capacity1);
// creating object of Intbuffer 2
// and allocating size capacity
IntBuffer ib2 = IntBuffer.allocate(capacity2);
// putting the value in Intbuffer 1
ib1.put( 8 );
ib1.put( 2 , 9 );
ib1.rewind();
// putting the value in Intbuffer 2
ib2.put( 8 );
ib2.put( 2 , 9 );
ib2.rewind();
// print the IntBuffer 1
System.out.println( " IntBuffer 1:  "
+ Arrays.toString(ib1.array()));
// print the IntBuffer 2
System.out.println( " IntBuffer 2:  "
+ Arrays.toString(ib2.array()));
// checking the equality of both IntBuffer
boolean ibb = ib1.equals(ib2);
// checking if else condition
if (ibb)
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" );
}
}
}


输出:

IntBuffer 1:  [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
 IntBuffer 2:  [8, 0, 9, 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 IntBuffer 1
int capacity1 = 10 ;
// Declaring the capacity of the  IntBuffer 2
int capacity2 = 10 ;
// Creating the IntBuffer
try {
// creating object of Intbuffer 1
// and allocating size capacity
IntBuffer ib1 = IntBuffer.allocate(capacity1);
// creating object of Intbuffer 2
// and allocating size capacity
IntBuffer ib2 = IntBuffer.allocate(capacity2);
// putting the value in Intbuffer 1
ib1.put( 8 );
ib1.put( 2 , 9 );
ib1.rewind();
// putting the value in Intbuffer 2
ib2.put( 8 );
ib2.put( 2 , 9 );
ib2.put( 3 , 7 );
ib2.put( 4 , 4 );
ib2.rewind();
// print the IntBuffer 1
System.out.println( " IntBuffer 1:  "
+ Arrays.toString(ib1.array()));
// print the IntBuffer 2
System.out.println( " IntBuffer 2:  "
+ Arrays.toString(ib2.array()));
// checking the equality of both IntBuffer
boolean ibb = ib1.equals(ib2);
// checking if else condition
if (ibb)
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" );
}
}
}


输出:

IntBuffer 1:  [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]
 IntBuffer 2:  [8, 0, 9, 7, 4, 0, 0, 0, 0, 0]
Both are not equal

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享