📜  Java中的 CharBuffer compareTo() 方法

📅  最后修改于: 2022-05-13 01:54:18.780000             🧑  作者: Mango

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');
  
            // 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