java.nio.IntBuffer类的compareTo()方法用于将一个缓冲区与另一个缓冲区进行比较。通过按字典顺序比较两个int缓冲区的其余元素序列来比较两个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
相关用法
- Java IntBuffer arrayOffset()用法及代码示例
- Java IntBuffer asReadOnlyBuffer()用法及代码示例
- Java IntBuffer compact()用法及代码示例
- Java IntBuffer equals()用法及代码示例
- Java IntBuffer hasArray()用法及代码示例
- Java IntBuffer array()用法及代码示例
- Java IntBuffer allocate()用法及代码示例
- Java IntBuffer slice()用法及代码示例
- Java IntBuffer wrap()用法及代码示例
- Java IntBuffer duplicate()用法及代码示例
- Java IntBuffer limit()用法及代码示例
- Java IntBuffer rewind()用法及代码示例
- Java IntBuffer flip()用法及代码示例
- Java IntBuffer clear()用法及代码示例
- Java IntBuffer mark()用法及代码示例
注:本文由纯净天空筛选整理自nitin_sharma大神的英文原创作品 IntBuffer compareTo() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。