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


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


java.nio.DoubleBuffer類的compareTo()方法用於將一個緩衝區與另一個緩衝區進行比較。通過按字典順序比較兩個剩餘緩衝區的剩餘元素序列來比較兩個雙緩衝區,而不考慮每個序列在其相應緩衝區中的開始位置。像調用Double.compare(double,double)一樣比較浮點對,不同之處在於-0.0和0.0被認為是相等的。此方法認為Double.NaN等於其自身,並且大於所有其他double值(包括Double.POSITIVE_INFINITY)。雙緩衝區與任何其他類型的對象都不具有可比性。

用法:

public int compareTo(DoubleBuffer that)

參數:此方法將doublebuffer對象作為參數,與之比較。


返回值:由於此緩衝區小於,等於或大於給定的緩衝區,因此此方法返回負整數,零或正整數。

下麵是說明compareTo()方法的示例:

範例1:當兩個DoubleBuffer相等時。

// 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 db 
        int capacity1 = 3; 
  
        // Creating the DoubleBuffer 
        try { 
  
            // creating object of Doublebuffer db 
            // and allocating size capacity 
            DoubleBuffer db = DoubleBuffer.allocate(capacity1); 
  
            // putting the value in db 
            db.put(9.56); 
            db.put(7.61); 
            db.put(4.61); 
  
            // revind the Double buffer 
            db.rewind(); 
  
            // print the DoubleBuffer 
            System.out.println("DoubleBuffer db: "
                               + Arrays.toString(db.array())); 
  
            // creating object of Doublebuffer db1 
            // and allocating size capacity 
            DoubleBuffer db1 = DoubleBuffer.allocate(capacity1); 
  
            // putting the value in db1 
            db1.put(9.56); 
            db1.put(7.61); 
            db1.put(4.61); 
  
            // revind the Double buffer 
            db1.rewind(); 
  
            // print the DoubleBuffer 
            System.out.println("DoubleBuffer db1: "
                               + Arrays.toString(db1.array())); 
  
            // compare both buffer and store the value into integer 
            int i = db.compareTo(db1); 
  
            // if else condition 
            if (i == 0) 
                System.out.println("\nboth buffer are lexicographically equal"); 
            else if (i >= 0) 
                System.out.println("\ndb is lexicographically greater than db1"); 
            else
                System.out.println("\ndb is lexicographically less than db1"); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("Exception throws : " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("Exception throws : " + e); 
        } 
    } 
}
輸出:
DoubleBuffer db: [9.56, 7.61, 4.61]
DoubleBuffer db1: [9.56, 7.61, 4.61]

both buffer are lexicographically equal

範例2:當此DoubleBuffer大於傳遞的DoubleBuffer時

// 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 db 
        int capacity1 = 3; 
  
        // Creating the DoubleBuffer 
        try { 
  
            // creating object of Doublebuffer db 
            // and allocating size capacity 
            DoubleBuffer db = DoubleBuffer.allocate(capacity1); 
  
            // putting the value in db 
            db.put(9.56); 
            db.put(7.61); 
            db.put(4.61); 
  
            // revind the Double buffer 
            db.rewind(); 
  
            // print the DoubleBuffer 
            System.out.println("DoubleBuffer db: "
                               + Arrays.toString(db.array())); 
  
            // creating object of Doublebuffer db1 
            // and allocating size capacity 
            DoubleBuffer db1 = DoubleBuffer.allocate(capacity1); 
  
            // putting the value in db1 
            db1.put(8.56); 
            db1.put(7.61); 
            db1.put(4.61); 
  
            // revind the Double buffer 
            db1.rewind(); 
  
            // print the DoubleBuffer 
            System.out.println("DoubleBuffer db1: "
                               + Arrays.toString(db1.array())); 
  
            // compare both buffer and store the value into integer 
            int i = db.compareTo(db1); 
  
            // if else condition 
            if (i == 0) 
                System.out.println("\nboth buffer are lexicographically equal"); 
            else if (i >= 0) 
                System.out.println("\ndb is lexicographically greater than db1"); 
            else
                System.out.println("\ndb is lexicographically less than db1"); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("Exception throws : " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("Exception throws : " + e); 
        } 
    } 
}
輸出:
DoubleBuffer db: [9.56, 7.61, 4.61]
DoubleBuffer db1: [8.56, 7.61, 4.61]

db is lexicographically greater than db1


相關用法


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