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


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


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

用法:

public int compareTo(IntBuffer that)

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


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

以下示例程序旨在說明compareTo()方法:

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

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

Both buffer are lexicographically equal

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

// 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 ib 
        int capacity1 = 3; 
  
        // Creating the IntBuffer 
        try { 
  
            // creating object of Intbuffer ib 
            // and allocating size capacity 
            IntBuffer ib = IntBuffer.allocate(capacity1); 
  
            // putting the value in ib 
            ib.put(9); 
            ib.put(7); 
            ib.put(4); 
  
            // revind the Int buffer 
            ib.rewind(); 
  
            // print the IntBuffer 
            System.out.println("IntBuffer ib: "
                               + Arrays.toString(ib.array())); 
  
            // creating object of Intbuffer ib1 
            // and allocating size capacity 
            IntBuffer ib1 = IntBuffer.allocate(capacity1); 
  
            // putting the value in ib1 
            ib1.put(8); 
            ib1.put(7); 
            ib1.put(4); 
  
            // revind the Int buffer 
            ib1.rewind(); 
  
            // print the IntBuffer 
            System.out.println("IntBuffer ib1: "
                               + Arrays.toString(ib1.array())); 
  
            // compare both buffer and store the value into integer 
            int i = ib.compareTo(ib1); 
  
            // if else condition 
            if (i == 0) 
                System.out.println("\nBoth buffer are lexicographically equal"); 
            else if (i >= 0) 
                System.out.println("\nib is lexicographically greater than ib1"); 
            else
                System.out.println("\nib is lexicographically less than ib1"); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("Exception throws : " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("Exception throws : " + e); 
        } 
    } 
}
輸出:
IntBuffer ib: [9, 7, 4]
IntBuffer ib1: [8, 7, 4]

ib is lexicographically greater than ib1

示例3:當此IntBuffer小於傳遞的IntBuffer時。

// 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 ib 
        int capacity1 = 3; 
  
        // Creating the IntBuffer 
        try { 
  
            // creating object of Intbuffer ib 
            // and allocating size capacity 
            IntBuffer ib = IntBuffer.allocate(capacity1); 
  
            // putting the value in ib 
            ib.put(8); 
            ib.put(7); 
            ib.put(4); 
  
            // revind the Int buffer 
            ib.rewind(); 
  
            // print the IntBuffer 
            System.out.println("IntBuffer ib: "
                               + Arrays.toString(ib.array())); 
  
            // creating object of Intbuffer ib1 
            // and allocating size capacity 
            IntBuffer ib1 = IntBuffer.allocate(capacity1); 
  
            // putting the value in ib1 
            ib1.put(9); 
            ib1.put(7); 
            ib1.put(4); 
  
            // revind the Int buffer 
            ib1.rewind(); 
  
            // print the IntBuffer 
            System.out.println("IntBuffer ib1: "
                               + Arrays.toString(ib1.array())); 
  
            // compare both buffer and store the value into integer 
            int i = ib.compareTo(ib1); 
  
            // if else condition 
            if (i == 0) 
                System.out.println("\nBoth buffer are lexicographically equal"); 
            else if (i >= 0) 
                System.out.println("\nib is lexicographically greater than ib1"); 
            else
                System.out.println("\nib is lexicographically less than ib1"); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("Exception throws : " + e); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("Exception throws : " + e); 
        } 
    } 
}
輸出:
IntBuffer ib: [8, 7, 4]
IntBuffer ib1: [9, 7, 4]

ib is lexicographically less than ib1


相關用法


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