當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java CharBuffer compareTo()用法及代碼示例


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


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 CharBuffer compareTo() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。