Java CharBuffer equals()方法
java.nio.CharBuffer 类的 equals() 方法用于检查给定的缓冲区是否与另一个对象相等。
两个char缓冲区是相等的,当且仅当。
- 它们具有相同的元素类型。
- 它们有相同数量的剩余元素,并且
- 两个剩余元素的序列,独立考虑它们的起始位置,是点状的 。
这个方法认为两个char元素’a’和’b’是相等的,如果(‘a’ == ‘b’) || (Char.isNaN(a) && Char.isNaN(b)) 。值-0和+0被认为是相等的,与Char.eals(Object)不同。
一个char缓冲区不等于任何其他类型的对象。
语法
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 CharBuffer 1
int capacity1 = 10;
// Declaring the capacity of the CharBuffer 2
int capacity2 = 10;
// Creating the CharBuffer
try {
// creating object of Charbuffer 1
// and allocating size capacity
CharBuffer cb1 = CharBuffer.allocate(capacity1);
// creating object of Charbuffer 2
// and allocating size capacity
CharBuffer cb2 = CharBuffer.allocate(capacity2);
// putting the value in Charbuffer 1
cb1.put('a');
cb1.put(2, 'b');
cb1.rewind();
// putting the value in Charbuffer 2
cb2.put('a');
cb2.put(2, 'b');
cb2.rewind();
// print the CharBuffer 1
System.out.println(" CharBuffer 1: "
+ Arrays.toString(cb1.array()));
// print the CharBuffer 2
System.out.println(" CharBuffer 2: "
+ Arrays.toString(cb2.array()));
// checking the equality of both CharBuffer
boolean cbb = cb1.equals(cb2);
// checking if else condition
if (cbb)
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");
}
}
}
输出:
CharBuffer 1: [a,, b,,,,,,, ]
CharBuffer 2: [a,, b,,,,,,, ]
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 CharBuffer 1
int capacity1 = 10;
// Declaring the capacity of the CharBuffer 2
int capacity2 = 5;
// Creating the CharBuffer
try {
// creating object of Charbuffer 1
// and allocating size capacity
CharBuffer cb1 = CharBuffer.allocate(capacity1);
// creating object of Charbuffer 2
// and allocating size capacity
CharBuffer cb2 = CharBuffer.allocate(capacity2);
// putting the value in Charbuffer 1
cb1.put('a');
cb1.put(2, 'b');
cb1.rewind();
// putting the value in Charbuffer 2
cb2.put('a');
cb2.put(2, 'b');
cb2.rewind();
// print the CharBuffer 1
System.out.println(" CharBuffer 1: "
+ Arrays.toString(cb1.array()));
// print the CharBuffer 2
System.out.println(" CharBuffer 2: "
+ Arrays.toString(cb2.array()));
// checking the equality of both CharBuffer
boolean cbb = cb1.equals(cb2);
// checking if else condition
if (cbb)
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");
}
}
}
输出:
CharBuffer 1: [a,, b,,,,,,, ]
CharBuffer 2: [a,, b,, ]
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 CharBuffer 1
int capacity1 = 10;
// Declaring the capacity of the CharBuffer 2
int capacity2 = 10;
// Creating the CharBuffer
try {
// creating object of Charbuffer 1
// and allocating size capacity
CharBuffer cb1 = CharBuffer.allocate(capacity1);
// creating object of Charbuffer 2
// and allocating size capacity
CharBuffer cb2 = CharBuffer.allocate(capacity2);
// putting the value in Charbuffer 1
cb1.put('a');
cb1.put(2, 'b');
cb1.rewind();
// putting the value in Charbuffer 2
cb2.put('a');
cb2.put(2, 'b');
cb2.put(3, 'c');
cb2.put(4, 'd');
cb2.rewind();
// print the CharBuffer 1
System.out.println(" CharBuffer 1: "
+ Arrays.toString(cb1.array()));
// print the CharBuffer 2
System.out.println(" CharBuffer 2: "
+ Arrays.toString(cb2.array()));
// checking the equality of both CharBuffer
boolean cbb = cb1.equals(cb2);
// checking if else condition
if (cbb)
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");
}
}
}
输出:
CharBuffer 1: [a,, b,,,,,,, ]
CharBuffer 2: [a,, b, c, d,,,,, ]
Both are not equal