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


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