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


Java ShortBuffer compareTo用法及代碼示例


java.nio.ShortBuffer類的compareTo()方法用於將一個緩衝區與另一個緩衝區進行比較。

通過按字典順序比較兩個短緩衝區的其餘元素序列來比較兩個短緩衝區,而不考慮每個序列在其相應緩衝區中的開始位置。通過調用Short.compare(short,short)來比較短元素對。短緩衝區不能與任何其他類型的對象進行比較。

用法


public int compareTo(ShortBuffer that)

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

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

下麵是說明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 sb 
        int capacity1 = 3; 
  
        // Creating the ShortBuffer 
        try { 
  
            // creating object of shortbuffer sb 
            // and allocating size capacity 
            ShortBuffer sb = ShortBuffer.allocate(capacity1); 
  
            // putting the value in sb 
            sb.put((short)956); 
            sb.put((short)761); 
            sb.put((short)461); 
  
            // revind the short buffer 
            sb.rewind(); 
  
            // print the ShortBuffer 
            System.out.println(" ShortBuffer sb: "
                               + Arrays.toString(sb.array())); 
  
            // creating object of shortbuffer sb1 
            // and allocating size capacity 
            ShortBuffer sb1 = ShortBuffer.allocate(capacity1); 
  
            // putting the value in sb1 
            sb1.put((short)956); 
            sb1.put((short)761); 
            sb1.put((short)461); 
  
            // revind the short buffer 
            sb1.rewind(); 
  
            // print the shortBuffer 
            System.out.println(" ShortBuffer sb1: "
                               + Arrays.toString(sb1.array())); 
  
            // compare both buffer and store the value into integer 
            int i = sb.compareTo(sb1); 
  
            // 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); 
        } 
    } 
}
輸出:
ShortBuffer sb: [956, 761, 461]
ShortBuffer sb1: [956, 761, 461]

both buffer are lexicographically equal

程序2:當此ShortBuffer大於傳遞的ShortBuffer時

// 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 sb 
        int capacity1 = 3; 
  
        // Creating the ShortBuffer 
        try { 
  
            // creating object of shortbuffer sb 
            // and allocating size capacity 
            ShortBuffer sb = ShortBuffer.allocate(capacity1); 
  
            // putting the value in sb 
            sb.put((short)956); 
            sb.put((short)761); 
            sb.put((short)41); 
  
            // revind the short buffer 
            sb.rewind(); 
  
            // print the ShortBuffer 
            System.out.println(" ShortBuffer s: "
                               + Arrays.toString(sb.array())); 
  
            // creating object of shortbuffer sb1 
            // and allocating size capacity 
            ShortBuffer sb1 = ShortBuffer.allocate(capacity1); 
  
            // putting the value in sb1 
            sb1.put((short)856); 
            sb1.put((short)761); 
            sb1.put((short)461); 
  
            // revind the short buffer 
            sb1.rewind(); 
  
            // print the ShortBuffer 
            System.out.println(" ShortBuffer sb1: "
                               + Arrays.toString(sb1.array())); 
  
            // compare both buffer and store the value into integer 
            int i = sb.compareTo(sb1); 
  
            // 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); 
        } 
    } 
}
輸出:
ShortBuffer s: [956, 761, 41]
ShortBuffer sb1: [856, 761, 461]

fb is lexicographically greater than fb1

程序3:當此ShortBuffer小於傳遞的ShortBuffer時

// 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 sb 
        int capacity1 = 3; 
  
        // Creating the ShortBuffer 
        try { 
  
            // creating object of shortbuffer sb 
            // and allocating size capacity 
            ShortBuffer sb = ShortBuffer.allocate(capacity1); 
  
            // putting the value in sb 
            sb.put((short)856); 
            sb.put((short)761); 
            sb.put((short)461); 
  
            // revind the short buffer 
            sb.rewind(); 
  
            // print the ShortBuffer 
            System.out.println(" ShortBuffer sb: "
                               + Arrays.toString(sb.array())); 
  
            // creating object of shortbuffer sb1 
            // and allocating size capacity 
            ShortBuffer sb1 = ShortBuffer.allocate(capacity1); 
  
            // putting the value in sb1 
            sb1.put((short)956); 
            sb1.put((short)761); 
            sb1.put((short)461); 
  
            // revind the short buffer 
            sb1.rewind(); 
  
            // print the ShortBuffer 
            System.out.println(" ShortBuffer sb1: "
                               + Arrays.toString(sb1.array())); 
  
            // compare both buffer and store the value into integer 
            int i = sb.compareTo(sb1); 
  
            // 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); 
        } 
    } 
}
輸出:
ShortBuffer sb: [856, 761, 461]
ShortBuffer sb1: [956, 761, 461]

fb is lexicographically less than fb1


相關用法


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