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


Java LongBuffer wrap()用法及代碼示例


wrap(Long[] array)

java.nio.LongBuffer類的wrap()方法用於包裝Long數組Longo緩衝區。新緩衝區將由給定的Long數組支持;也就是說,對緩衝區的修改將導致數組被修改,反之亦然。新緩衝區的容量和限製為array.length,位置為零,標記未定義。它的支持數組將是給定的數組,其數組偏移量將為零。

用法:

public static LongBuffer wrap(Long[] array)

參數:此方法將array(將支持此緩衝區的數組)作為參數。


返回值:此方法返回新的Long緩衝區。

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

程序1

// Java program to demonstrate 
// asReadOnlyBuffer() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declare and initialize the Long array 
        long[] ibb = { 1L, 2L, 3L }; 
  
        // prLong the Long array length 
        System.out.println("Array length : " + ibb.length); 
  
        // prLong the Long array element 
        System.out.println("\nArray element : "
                           + Arrays.toString(ibb)); 
  
        // wrap the Long array Longo LongBuffer 
        // using wrap() method 
        LongBuffer longBuffer = LongBuffer.wrap(ibb, 0, ibb.length); 
  
        // Rewind the Longbuffer 
        longBuffer.rewind(); 
  
        // prLong the Long buffer 
        System.out.println("\nlongBuffer : "
                           + Arrays.toString(longBuffer.array())); 
  
        // prLong the LongBuffer capacity 
        System.out.println("\nlongbuffer capacity : "
                           + longBuffer.capacity()); 
  
        // prLong the LongBuffer position 
        System.out.println("\nlongbuffer position: "
                           + longBuffer.position()); 
    } 
}
輸出:
Array length : 3

Array element : [1, 2, 3]

LongBuffer : [1, 2, 3]

Longbuffer capacity : 3

Longbuffer position:  0

wrap(Long[] array, Long offset, Long length)

新緩衝區將由給定的Long數組支持;也就是說,對緩衝區的修改將導致數組被修改,反之亦然。新緩衝區的容量將為array.length,其位置將偏移,其限製將為offset + length,其標記將是不確定的。它的支持數組將是給定的數組,其數組偏移量將為零。

用法:

public static LongBuffer wrap (Long[] array, Long offset, Long length)

參數:此方法采用以下參數:

  • array:將支持新緩衝區的數組。
  • offset:要使用的子數組的偏移量;必須為非負數,且不得大於array.length。新緩衝區的位置將設置為此值。
  • length:要使用的子數組的長度;必須為非負且不大於array.length –偏移量。新緩衝區的限製將設置為偏移量+長度。

返回值:此方法返回新的浮點緩衝區。

拋出:此方法拋出IndexOutOfBoundsException(如果offset和length參數的前提條件不成立)。

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

示例1:

// Java program to demonstrate 
// asReadOnlyBuffer() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declare and initialize the Long array 
        long[] ibb = { 1, 2, 3 }; 
  
        // prLong the Long array length 
        System.out.println("Array length : " + ibb.length); 
  
        // prLong the Long array element 
        System.out.println("\nArray element : "
                           + Arrays.toString(ibb)); 
  
        // wrap the Long array Longo LongBuffer 
        // using wrap() method 
        LongBuffer longBuffer = LongBuffer.wrap(ibb, 0, 
                                                ibb.length); 
  
        // Rewind the Longbuffer 
        longBuffer.rewind(); 
  
        // prLong the Long buffer 
        System.out.println("\nLongBuffer : "
                           + Arrays.toString(longBuffer.array())); 
  
        // prLong the LongBuffer capacity 
        System.out.println("\nLongbuffer capacity : "
                           + longBuffer.capacity()); 
  
        // prLong the LongBuffer position 
        System.out.println("\nLongbuffer position: "
                           + longBuffer.position()); 
    } 
}
輸出:
Array length : 3

Array element : [1, 2, 3]

LongBuffer : [1, 2, 3]

Longbuffer capacity : 3

Longbuffer position:  0

示例2:演示NullPoLongerException。

// Java program to demonstrate 
// asReadOnlyBuffer() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declare and initialize the float array 
        long[] ibb = { 1, 2, 3 }; 
  
        // prLong the Long array length 
        System.out.println("Array length : " + ibb.length); 
  
        // prLong the Long array element 
        System.out.println("\nArray element : " + Arrays.toString(ibb)); 
  
        try { 
            // wrap the Long array Longo LongBuffer 
            // using wrap() method 
            System.out.println("\nHere "
                               + "offset and length does not hold"
                               + " the required condition "); 
            LongBuffer longBuffer = LongBuffer.wrap(ibb, 
                                                    1, 
                                                    ibb.length); 
  
            // Rewind the Longbuffer 
            longBuffer.rewind(); 
  
            // prLong the Long buffer 
            System.out.println("\nLongBuffer : "
                               + Arrays.toString(longBuffer.array())); 
  
            // prLong the LongBuffer capacity 
            System.out.println("\nLongbuffer capacity : "
                               + longBuffer.capacity()); 
  
            // prLong the LongBuffer position 
            System.out.println("\nLongbuffer position:  "
                               + longBuffer.position()); 
        } 
        catch (IndexOutOfBoundsException e) { 
            System.out.println("Exception throws:  " + e); 
        } 
    } 
}
輸出:
Array length : 3

Array element : [1, 2, 3]

Here offset and length does not hold the required condition 
Exception throws:  java.lang.IndexOutOfBoundsException


相關用法


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