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


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


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

用法:

public int compareTo(FloatBuffer that)

參數:此方法將floatbuffer對象作為參數,與該緩衝區進行比較。


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

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

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

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

fb is lexicographically greater than fb1

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

fb is lexicographically less than fb1


相關用法


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