Java中的 IntBuffer compareTo() 方法
Java.nio.IntBuffer类的compareTo()方法用于将一个缓冲区与另一个缓冲区进行比较。通过按字典顺序比较它们的剩余元素序列来比较两个 int 缓冲区,而不考虑每个序列在其相应缓冲区中的起始位置。对 int 元素的比较就像通过调用 Int.compare(int, int) 一样,除了 -0 和 0 被认为是相等的。此方法认为 Int.NaN 等于其自身并且大于所有其他 int 值(包括 Int.POSITIVE_INFINITY)。 int 缓冲区无法与任何其他类型的对象进行比较。
句法 :
public int compareTo(IntBuffer that)
参数:此方法将intbuffer 对象作为参数,与此缓冲区进行比较。
返回值:此方法返回负整数、零或正整数,因为此缓冲区小于、等于或大于给定缓冲区。
下面的程序说明了compareTo()方法:
示例 1:当两个 IntBuffer 相等时。
// Java program to demonstrate
// compareTo() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ib
int capacity1 = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer ib
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity1);
// putting the value in ib
ib.put(9);
ib.put(7);
ib.put(4);
// revind the Int buffer
ib.rewind();
// print the IntBuffer
System.out.println("IntBuffer ib: "
+ Arrays.toString(ib.array()));
// creating object of Intbuffer ib1
// and allocating size capacity
IntBuffer ib1 = IntBuffer.allocate(capacity1);
// putting the value in ib1
ib1.put(9);
ib1.put(7);
ib1.put(4);
// revind the Int buffer
ib1.rewind();
// print the IntBuffer
System.out.println("IntBuffer ib1: "
+ Arrays.toString(ib1.array()));
// compare both buffer and store the value into integer
int i = ib.compareTo(ib1);
// if else condition
if (i == 0)
System.out.println("\nBoth buffer are lexicographically equal");
else if (i >= 0)
System.out.println("\nib is lexicographically greater than ib1");
else
System.out.println("\nib is lexicographically less than ib1");
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
IntBuffer ib: [9, 7, 4]
IntBuffer ib1: [9, 7, 4]
Both buffer are lexicographically equal
示例 2:当这个 IntBuffer 大于传递的 IntBuffer
// Java program to demonstrate
// compareTo() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ib
int capacity1 = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer ib
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity1);
// putting the value in ib
ib.put(9);
ib.put(7);
ib.put(4);
// revind the Int buffer
ib.rewind();
// print the IntBuffer
System.out.println("IntBuffer ib: "
+ Arrays.toString(ib.array()));
// creating object of Intbuffer ib1
// and allocating size capacity
IntBuffer ib1 = IntBuffer.allocate(capacity1);
// putting the value in ib1
ib1.put(8);
ib1.put(7);
ib1.put(4);
// revind the Int buffer
ib1.rewind();
// print the IntBuffer
System.out.println("IntBuffer ib1: "
+ Arrays.toString(ib1.array()));
// compare both buffer and store the value into integer
int i = ib.compareTo(ib1);
// if else condition
if (i == 0)
System.out.println("\nBoth buffer are lexicographically equal");
else if (i >= 0)
System.out.println("\nib is lexicographically greater than ib1");
else
System.out.println("\nib is lexicographically less than ib1");
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
IntBuffer ib: [9, 7, 4]
IntBuffer ib1: [8, 7, 4]
ib is lexicographically greater than ib1
示例 3:当此 IntBuffer 小于传递的 IntBuffer 时。
// Java program to demonstrate
// compareTo() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ib
int capacity1 = 3;
// Creating the IntBuffer
try {
// creating object of Intbuffer ib
// and allocating size capacity
IntBuffer ib = IntBuffer.allocate(capacity1);
// putting the value in ib
ib.put(8);
ib.put(7);
ib.put(4);
// revind the Int buffer
ib.rewind();
// print the IntBuffer
System.out.println("IntBuffer ib: "
+ Arrays.toString(ib.array()));
// creating object of Intbuffer ib1
// and allocating size capacity
IntBuffer ib1 = IntBuffer.allocate(capacity1);
// putting the value in ib1
ib1.put(9);
ib1.put(7);
ib1.put(4);
// revind the Int buffer
ib1.rewind();
// print the IntBuffer
System.out.println("IntBuffer ib1: "
+ Arrays.toString(ib1.array()));
// compare both buffer and store the value into integer
int i = ib.compareTo(ib1);
// if else condition
if (i == 0)
System.out.println("\nBoth buffer are lexicographically equal");
else if (i >= 0)
System.out.println("\nib is lexicographically greater than ib1");
else
System.out.println("\nib is lexicographically less than ib1");
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
IntBuffer ib: [8, 7, 4]
IntBuffer ib1: [9, 7, 4]
ib is lexicographically less than ib1