当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。