📜  Java中的 LongBuffer wrap() 方法

📅  最后修改于: 2022-05-13 01:54:41.495000             🧑  作者: Mango

Java中的 LongBuffer wrap() 方法

换行(长 [] 数组)

Java.nio.LongBuffer类的wrap()方法用于包装一个Long 数组,Longo,一个缓冲区。新缓冲区将由给定的 Long 数组支持;也就是说,对缓冲区的修改将导致数组被修改,反之亦然。新缓冲区的容量和限制将为 array.length,其位置为零,其标记未定义。它的后备数组将是给定的数组,并且它的数组偏移量将为零。

句法 :

public static LongBuffer wrap(Long[] 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[] 数组,长偏移,长长度)

新缓冲区将由给定的 Long 数组支持;也就是说,对缓冲区的修改将导致数组被修改,反之亦然。新缓冲区的容量为array.length,其位置为偏移量,其限制为偏移量+长度,其标记为未定义。它的后备数组将是给定的数组,并且它的数组偏移量将为零。

句法 :

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

参数:此方法采用以下参数:

  • 数组:将支持新缓冲区的数组。
  • offset:要使用的子数组的偏移量;必须为非负且不大于 array.length。新缓冲区的位置将设置为此值。
  • length:要使用的子数组的长度;必须为非负数且不大于 array.length – 偏移量。新缓冲区的限制将设置为偏移量 + 长度。

返回值:此方法返回新的浮点缓冲区。

抛出:此方法抛出IndexOutOfBoundsException (如果偏移量和长度参数的前提条件不成立)。

下面的程序说明了 wrap() 方法:

方案一:

// 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