java.nio.charBuffer类的compareTo()方法用于将一个缓冲区与另一个缓冲区进行比较。通过按字典顺序比较剩余字符的序列比较两个char缓冲区,而无需考虑每个序列在其相应缓冲区中的开始位置。通过调用Char.compare(char,char)比较成对的char元素。任何其他类型的对象都无法与char缓冲区进行比较。
用法:
public int compareTo(CharBuffer that)
参数:此方法将charbuffer对象作为参数,将与该缓冲区进行比较。
返回值:由于此缓冲区小于,等于或大于给定的缓冲区,因此此方法返回负整数,零或正整数。
下面是说明compareTo()方法的示例:
示例1:当两个CharBuffer相等时。
// 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 cb
int capacity1 = 3;
// Creating the CharBuffer
try {
// creating object of charbuffer cb
// and allocating size capacity
CharBuffer cb = CharBuffer.allocate(capacity1);
// putting the value in cb
cb.put('a');
cb.put('b');
cb.put('c');
// revind the float buffer
cb.rewind();
// print the Charbuffer
System.out.println("Charbuffer cb: "
+ Arrays.toString(cb.array()));
// creating object of floatbuffer cb1
// and allocating size capacity
CharBuffer cb1 = CharBuffer.allocate(capacity1);
// putting the value in cb1
cb1.put('a');
cb1.put('b');
cb1.put('c');
// revind the float buffer
cb1.rewind();
// print the Charbuffer
System.out.println("Charbuffer cb1: "
+ Arrays.toString(cb1.array()));
// compare both buffer and store the value into integer
int i = cb.compareTo(cb1);
// if else condition
if (i == 0)
System.out.println("\nboth buffer are"
+ "lexicographically equal");
else if (i >= 0)
System.out.println("\ncb is lexicographically"
+ "greater than cb1");
else
System.out.println("\ncb is lexicographically"
+ "less than cb1");
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
Charbuffer cb: [a, b, c] Charbuffer cb1: [a, b, c] both buffer arelexicographically equal
示例2:当此FloatBuffer大于传递的FloatBuffer时
// 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 cb
int capacity1 = 3;
// Creating the CharBuffer
try {
// creating object of floatbuffer cb
// and allocating size capacity
CharBuffer cb = CharBuffer.allocate(capacity1);
// putting the value in cb
cb.put('g');
cb.put('b');
cb.put('c');
// revind the float buffer
cb.rewind();
// print the CharBuffer
System.out.println("CharBuffer cb: "
+ Arrays.toString(cb.array()));
// creating object of floatbuffer cb1
// and allocating size capacity
CharBuffer cb1 = CharBuffer.allocate(capacity1);
// putting the value in cb1
cb1.put('a');
cb1.put('b');
cb1.put('c');
// revind the float buffer
cb1.rewind();
// print the CharBuffer
System.out.println("CharBuffer cb1: "
+ Arrays.toString(cb1.array()));
// compare both buffer and store
// the value into integer
int i = cb.compareTo(cb1);
// if else condition
if (i == 0)
System.out.println("\nboth buffer are"
+ " lexicographically equal");
else if (i >= 0)
System.out.println("\ncb is lexicographically"
+ " greater than cb1");
else
System.out.println("\ncb is lexicographically"
+ " less than cb1");
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
CharBuffer cb: [g, b, c] CharBuffer cb1: [a, b, c] cb is lexicographically greater than cb1
示例3:当此FloatBuffer小于传递的FloatBuffer时
// 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 cb
int capacity1 = 3;
// Creating the CharBuffer
try {
// creating object of CharBuffer cb
// and allocating size capacity
CharBuffer cb = CharBuffer.allocate(capacity1);
// putting the value in cb
cb.put('a');
cb.put('b');
cb.put('c');
// revind the float buffer
cb.rewind();
// print the CharBuffer
System.out.println("CharBuffer cb: "
+ Arrays.toString(cb.array()));
// creating object of CharBuffer cb1
// and allocating size capacity
CharBuffer cb1 = CharBuffer.allocate(capacity1);
// putting the value in cb1
cb1.put('g');
cb1.put('b');
cb1.put('c');
// revind the float buffer
cb1.rewind();
// print the CharBuffer
System.out.println("CharBuffer cb1: "
+ Arrays.toString(cb1.array()));
// compare both buffer and store the value into integer
int i = cb.compareTo(cb1);
// if else condition
if (i == 0)
System.out.println("\nboth buffer are"
+ "lexicographically equal");
else if (i >= 0)
System.out.println("\ncb is lexicographically"
+ "greater than cb1");
else
System.out.println("\ncb is lexicographically"
+ "less than cb1");
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws : " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws : " + e);
}
}
}
输出:
CharBuffer cb: [a, b, c] CharBuffer cb1: [g, b, c] cb is lexicographicallyless than cb1
相关用法
- Java CharBuffer equals()用法及代码示例
- Java CharBuffer array()用法及代码示例
- Java CharBuffer asReadOnlyBuffer()用法及代码示例
- Java CharBuffer slice()用法及代码示例
- Java CharBuffer wrap()用法及代码示例
- Java CharBuffer compact()用法及代码示例
- Java CharBuffer hasArray()用法及代码示例
- Java CharBuffer duplicate()用法及代码示例
- Java CharBuffer arrayOffset()用法及代码示例
- Java CharBuffer allocate()用法及代码示例
- Java CharArrayReader read(CharBuffer)用法及代码示例
- Java StringReader read(CharBuffer)用法及代码示例
- Java Reader read(CharBuffer)用法及代码示例
- Java CharsetEncoder encode(CharBuffer in)用法及代码示例
- Java Integer compareTo()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 CharBuffer compareTo() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。