Java CharBuffer compareTo()方法

Java CharBuffer compareTo()方法

java.nio.charBuffer 类的 compareTo() 方法是用来比较一个缓冲区和另一个缓冲区。两个char缓冲区通过比较它们的剩余元素序列进行比较,不考虑每个序列在其相应缓冲区中的起始位置。一对char元素的比较就像调用Char.compare(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');
 
            // rewind 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');
 
            // rewind 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');
 
            // rewind 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');
 
            // rewind 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');
 
            // rewind 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');
 
            // rewind 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南