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


Java ShortBuffer equals()用法及代碼示例


java.nio.ShortBuffer類的equals()方法用於檢查給定的緩衝區是否等於另一個對象。

當且僅當兩個短緩衝區相等時,

  • 它們具有相同的元素類型,
  • 它們具有相同數量的剩餘元素,並且
  • 其餘元素的兩個序列(與它們的起始位置無關)是按點排列的
    等於。

短緩衝區不等於任何其他類型的對象。


用法

public boolean equals(Object ob)

參數:此方法將ob(與該緩衝區進行比較的對象)作為參數。

返回值:僅當此緩衝區等於給定對象時,此方法返回true。

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

程序1

// Java program to demonstrate 
// equals() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ShortBuffer 1 
        int capacity1 = 10; 
  
        // Declaring the capacity of the ShortBuffer 2 
        int capacity2 = 10; 
  
        // Creating the ShortBuffer 
        try { 
  
            // creating object of Shortbuffer 1 
            // and allocating size capacity 
            ShortBuffer sb1 = ShortBuffer.allocate(capacity1); 
  
            // creating object of Shortbuffer 2 
            // and allocating size capacity 
            ShortBuffer fb2 = ShortBuffer.allocate(capacity2); 
  
            // putting the value in Shortbuffer 1 
            sb1.put((short)856); 
            sb1.put(2, (short)961); 
            sb1.rewind(); 
  
            // putting the value in Shortbuffer 2 
            fb2.put((short)856); 
            fb2.put(2, (short)961); 
            fb2.rewind(); 
  
            // print the ShortBuffer 1 
            System.out.println(" ShortBuffer 1: "
                               + Arrays.toString(sb1.array())); 
  
            // print the ShortBuffer 2 
            System.out.println(" ShortBuffer 2: "
                               + Arrays.toString(fb2.array())); 
  
            // checking the equality of both ShortBuffer 
            boolean fbb = sb1.equals(fb2); 
  
            // checking if else condition 
            if (fbb) 
                System.out.println("both are equal"); 
            else
                System.out.println("both are not equal"); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
輸出:
ShortBuffer 1: [856, 0, 961, 0, 0, 0, 0, 0, 0, 0]
ShortBuffer 2: [856, 0, 961, 0, 0, 0, 0, 0, 0, 0]

both are equal

程序2

// Java program to demonstrate 
// equals() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ShortBuffer 1 
        int capacity1 = 10; 
  
        // Declaring the capacity of the ShortBuffer 2 
        int capacity2 = 5; 
  
        // Creating the ShortBuffer 
        try { 
  
            // creating object of Shortbuffer 1 
            // and allocating size capacity 
            ShortBuffer sb1 = ShortBuffer.allocate(capacity1); 
  
            // creating object of Shortbuffer 2 
            // and allocating size capacity 
            ShortBuffer sb2 = ShortBuffer.allocate(capacity2); 
  
            // putting the value in Shortbuffer 1 
            sb1.put((short)856); 
            sb1.put(2, (short)961); 
            sb1.rewind(); 
  
            // putting the value in Shortbuffer 2 
            sb2.put((short)856); 
            sb2.put(2, (short)431); 
            sb2.rewind(); 
  
            // print the ShortBuffer 1 
            System.out.println(" ShortBuffer 1: "
                               + Arrays.toString(sb1.array())); 
  
            // print the ShortBuffer 2 
            System.out.println(" ShortBuffer 2: "
                               + Arrays.toString(sb2.array())); 
  
            // checking the equality of both ShortBuffer 
            boolean fbb = sb1.equals(sb2); 
  
            // checking if else condition 
            if (fbb) 
                System.out.println("both are equal"); 
            else
                System.out.println("both are not equal"); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
輸出:
ShortBuffer 1: [856, 0, 961, 0, 0, 0, 0, 0, 0, 0]
ShortBuffer 2: [856, 0, 431, 0, 0]

both are not equal


相關用法


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