当前位置: 首页>>代码示例>>用法及示例精选>>正文


Java IntBuffer wrap()用法及代码示例

wrap(int[] array)

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

用法:

public static IntBuffer wrap(int[] array)

参数:此方法将要支持此缓冲区的数组array作为参数。


返回值:此方法返回创建的新的int缓冲区。

下面是说明wrap()方法的示例:

范例1:

// Java program to demonstrate 
// wrap() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Declare and initialize the int array 
        int[] ibb = { 1, 2, 3 }; 
  
        // print the int array length 
        System.out.println("Array length : "
                           + ibb.length); 
  
        // print the int array element 
        System.out.println("\nArray element : "
                           + Arrays.toString(ibb)); 
  
        // wrap the int array into intBuffer 
        // using wrap() method 
        IntBuffer intBuffer = IntBuffer.wrap(ibb); 
  
        // Rewind the intbuffer 
        intBuffer.rewind(); 
  
        // print the int buffer 
        System.out.println("\nintBuffer : "
                           + Arrays.toString(intBuffer.array())); 
  
        // print the IntBuffer capacity 
        System.out.println("\nintbuffer capacity : "
                           + intBuffer.capacity()); 
  
        // print the IntBuffer position 
        System.out.println("\nintbuffer position:  "
                           + intBuffer.position()); 
    } 
}
输出:
Array length : 3

Array element : [1, 2, 3]

intBuffer : [1, 2, 3]

intbuffer capacity : 3

intbuffer position:  0

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

wrap()方法将int数组包装到缓冲区中。新缓冲区将由给定的int数组支持;也就是说,对缓冲区的修改将导致数组被修改,反之亦然。新缓冲区的容量将为array.length,其位置将偏移,其限制将为offset + length,其标记将是不确定的。它的支持数组将是给定的数组,其数组偏移量将为零。

用法:

public static IntBuffer 
    wrap (int[] array, int offset, int length)

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

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

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

引发:如果offset和length参数的前提条件不成立,则此方法引发IndexOutOfBoundsException。

下面是说明wrap()方法的示例:

范例1:

// Java program to demonstrate 
// wrap() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declare and initialize the int array 
        int[] ibb = { 1, 2, 3 }; 
  
        // print the int array length 
        System.out.println("Array length : " + ibb.length); 
  
        // print the int array element 
        System.out.println("\nArray element : "
                           + Arrays.toString(ibb)); 
  
        // wrap the int array into intBuffer 
        // using wrap() method 
        IntBuffer intBuffer = IntBuffer.wrap(ibb, 0, 
                                             ibb.length); 
  
        // Rewind the intbuffer 
        intBuffer.rewind(); 
  
        // print the int buffer 
        System.out.println("\nintBuffer : "
                           + Arrays.toString(intBuffer.array())); 
  
        // print the IntBuffer capacity 
        System.out.println("\nintbuffer capacity : "
                           + intBuffer.capacity()); 
  
        // print the IntBuffer position 
        System.out.println("\nintbuffer position: "
                           + intBuffer.position()); 
    } 
}
输出:
Array length : 3

Array element : [1, 2, 3]

intBuffer : [1, 2, 3]

intbuffer capacity : 3

intbuffer position: 0

范例2:演示NullPointerException

// Java program to demonstrate 
// wrap() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declare and initialize the float array 
        int[] ibb = { 1, 2, 3 }; 
  
        // print the int array length 
        System.out.println("Array length : "
                           + ibb.length); 
  
        // print the int array element 
        System.out.println("\nArray element : "
                           + Arrays.toString(ibb)); 
  
        try { 
            // wrap the int array into intBuffer 
            // using wrap() method 
            System.out.println("\nHere "
                               + "offset and length does not hold"
                               + " the required condition "); 
  
            IntBuffer intBuffer = IntBuffer.wrap(ibb, 
                                                 1, 
                                                 ibb.length); 
  
            // Rewind the intbuffer 
            intBuffer.rewind(); 
  
            // print the int buffer 
            System.out.println("\nintBuffer : "
                               + Arrays.toString(intBuffer.array())); 
  
            // print the IntBuffer capacity 
            System.out.println("\nintbuffer capacity : "
                               + intBuffer.capacity()); 
  
            // print the IntBuffer position 
            System.out.println("\nintbuffer position:  "
                               + intBuffer.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


相关用法

注:本文由纯净天空筛选整理自nitin_sharma大神的英文原创作品 IntBuffer wrap() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。