Java IntBuffer wrap()方法

Java IntBuffer wrap()方法

wrap(int[] array)

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

语法

public static IntBuffer wrap(int[] array)
Java

参数: 该方法以 数组 作为参数,数组将支持这个缓冲区。

返回值: 该方法返回新创建的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());
    }
}
Java

输出。

Array length : 3

Array element : [1, 2, 3]

intBuffer : [1, 2, 3]

intbuffer capacity : 3

intbuffer position:  0
Java

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

wrap()方法将一个int数组包装成一个缓冲区。新的缓冲区将由给定的int数组支持;也就是说,对缓冲区的修改将导致数组的修改,反之亦然。新缓冲区的容量是array.length,它的位置是offset,它的极限是offset + length,它的标记是undefined。它的支持数组将是给定的数组,它的数组偏移将是0。

语法

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

参数: 该方法需要以下参数。

  • array: 将支持新缓冲区的数组。
  • offset: 要使用的子数组的偏移量;必须是非负数,并且不大于array.length。新缓冲区的位置将被设置为这个值。
  • length: 要使用的子数组的长度;必须是非负数,并且不大于array.length – offset。新缓冲区的极限将被设置为offset + 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());
    }
}
Java

输出。

Array length : 3

Array element : [1, 2, 3]

intBuffer : [1, 2, 3]

intbuffer capacity : 3

intbuffer position: 0
Java

实例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);
        }
    }
}
Java

输出。

Array length : 3

Array element : [1, 2, 3]

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册