java.nio.IntBuffer類的equals()方法用於檢查給定的緩衝區是否等於另一個對象。
當且僅當兩個int緩衝區相等時,
- 它們具有相同的元素類型,
- 它們具有相同數量的剩餘元素,並且
- 其餘元素的兩個序列(與它們的起始位置無關)是按點排列的
等於。
如果(a == b)||,則此方法認為兩個int元素a和b相等。 (Int.isNaN(a)&& Int.isNaN(b))。與Int.equals(Object)不同,值-0和+0被視為相等。
一個int緩衝區不等於任何其他類型的對象。
用法:
public boolean equals(Object ob)
參數:此方法將ob(與該緩衝區進行比較的對象)作為參數。
返回值:僅當此緩衝區等於給定對象時,此方法返回true。
下麵是說明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 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
相關用法
- Java IntBuffer slice()用法及代碼示例
- Java IntBuffer wrap()用法及代碼示例
- Java IntBuffer array()用法及代碼示例
- Java IntBuffer compareTo()用法及代碼示例
- Java IntBuffer hasArray()用法及代碼示例
- Java IntBuffer arrayOffset()用法及代碼示例
- Java IntBuffer allocate()用法及代碼示例
- Java IntBuffer asReadOnlyBuffer()用法及代碼示例
- Java IntBuffer compact()用法及代碼示例
- Java IntBuffer duplicate()用法及代碼示例
- Java IntBuffer limit()用法及代碼示例
- Java IntBuffer rewind()用法及代碼示例
- Java IntBuffer flip()用法及代碼示例
- Java IntBuffer mark()用法及代碼示例
- Java IntBuffer clear()用法及代碼示例
注:本文由純淨天空篩選整理自nitin_sharma大神的英文原創作品 IntBuffer equals() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。